Click here to Skip to main content
Licence CPOL
First Posted 25 Feb 2008
Views 22,347
Bookmarked 27 times

How to Load Windows Form From Data Base

By | 2 Mar 2008 | Article
How to Load Windows Form From Data Base

How to Load Windows Form From Data Base

Some times we want to save the form name in data base and load form using that saved name. How would we achieve this task? In 2005 there is Name space called “System.Reflection” This name space support dynamically creating types.

  Private Function LoadFormFromDataBase(ByVal FormName As String) As Object

1        Dim oRetForm As Object = Nothing
2
3        Dim oType As Type =   
4        Assembly.GetExecutingAssembly().GetType("MyProject." & FormName)
5
6        If oType IsNot Nothing Then
7            oRetForm = Activator.CreateInstance(oType)
8        End If
9
10       Return oRetForm
11
    End Function

 
		

This function has one argument it is a Form name. In line 3 and 5 you can see our form name (the argument we passed to load the form) dynamically assembling in our memory. For this instance there is on big important thing, that was your Project what is the Project your form containing. Also another very, very important thing is “.” after your Project Name. That is the separator acting between Project name and the Form name.

So we now we are going to implement that instance in our memory (else you can create it remotely) . This task doing Activator class using CreateInstance method in line 7. This CreateInstance method 14 arguments but for this task we just passing object type.

So now you have form in your object type variable (oRetForm), Using this method you can load other controls as well.

Review

Ok I got few comments of my friends, first I would like to thanks all of your comments. Yes actually this windows form not saved in your data base actually data base contain your form name (Keep it mind not a form text). So lets see why we want this kind of things.

ok you have tree node, dynamically load from data base.(Node name, root, etc) so when you click that node you have to open a form. for that you have two option one is get a node name and load form, for that you have to hard code your form name like this


Private Sub treMain_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treMain.AfterSelect

With e.Node

            If .Name = "A" Then

                Dim OfrmA As New frmA
                frmA.Show()

            ElseIf .Name = "B" Then
                Dim OfrmB As New frmB
                OfrmB.Show()

            ElseIf .Name = "C" Then

                Dim OfrmC As New frmC
                OfrmC.Show()

            ElseIf .Name = "D" Then

                Dim OfrmD As New frmD
                OfrmD.Show()

            ElseIf .Name = "E" Then

                Dim OfrmE As New frmE
                OfrmE.Show()

            ElseIf .Name = "F" Then

                Dim OfrmF As New frmF
                OfrmF.Show()

            ElseIf .Name = "G" Then

                Dim OfrmG As New frmG
                OfrmF.Show()

            End If

        End With

End Sub

other way is in your Node Table you have to create extra column for contain a form name for that node. so when you loading that tree node you can add form name for the tree node name. so now you have your form names in your tree view's node names.

now just use this code


Private Sub treMain_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treMain.AfterSelect

Dim frmUnLoad As New Form
frmLoad = LoadFormFromDataBase(e.Node.Name)

End Sub

 Private Function LoadFormFromDataBase(ByVal objectName As String) As Object


Dim returnObj As Object = Nothing
Dim type As Type = Assembly.GetExecutingAssembly().GetType("UserInterface." & objectName)

        If type IsNot Nothing Then
            returnObj = Activator.CreateInstance(type)
        End If

        Return returnObj

 End Function

That’s it. Now you can load your form when you clicking the node.

So do you want edit your code if you want add new form and add new node for the solution, no need , create a form add to your solution and add a record to your node table with form name that it.

Which one is better solution? Decision is on your side

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gayan Buddhika

Software Developer (Senior)

New Zealand New Zealand

Member

Gayan worked as a Senor Software Engineer and a Team Lead in AmSafe Bridport. He has completed his Master Degree in IT and he is a Microsoft Certified Technology Specialist in SharePoint 2010 application development. Gayan has more than 7 years of software development experience with Microsoft technologies including .NET, SQL, SharePoint, IIS, Silverlight, Expression Blend 4 and many more. Currently he is reading for his Postgraduate Diploma in Business Administration at AIS in Auckland, New Zealand. Furthermore he is servicing as a part time consultant with MStechnology in Auckland.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA bit harsh Pinmemberwestville_mike2:50 16 Nov '09  
GeneralDoes not respond on VB.Net 2008 PinmemberMember 16763913:13 7 Sep '09  
GeneralRe: Does not respond on VB.Net 2008 PinmemberGayan Buddhika17:34 7 Sep '09  
GeneralRe: Does not respond on VB.Net 2008 PinmemberMember 167639123:09 7 Sep '09  
GeneralTo all who criticise saying it bad PinmemberIrwan Hassan2:32 27 Jun '09  
GeneralMy vote of 1 PinmemberJon_Boy8:24 3 Apr '09  
GeneralConstructive criticism PinmemberBert delaVega6:07 3 Mar '08  
GeneralUmmm, there is no reference to any database code whatsoever PinmemberDelphiCoder13:33 29 Feb '08  
How can you state that you are posting something about databases, when you don't touch on the subject at all?
 
Also, all your code does is to create a form in the most convoluted way imaginable.
GeneralSurprisingly irrelevant PinmemberAlbert Dadze20:31 27 Feb '08  
GeneralThis is one,.. PinmemberBert delaVega7:30 26 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 2 Mar 2008
Article Copyright 2008 by Gayan Buddhika
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid