Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / Visual Basic
Article

How to Load Windows Form From Data Base

Rate me:
Please Sign up or sign in to vote.
1.19/5 (18 votes)
2 Mar 2008CPOL2 min read 45K   30   14
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)



Comments and Discussions

 
QuestionHow to Load Windows Form From Data Base Pin
rasinravi24-Nov-14 23:08
rasinravi24-Nov-14 23:08 
AnswerRe: How to Load Windows Form From Data Base Pin
Gayan Buddhika30-Nov-14 16:43
professionalGayan Buddhika30-Nov-14 16:43 
Questionperfect.. that´s what I was searching for Pin
Stephan.Zeller29-Nov-12 1:36
Stephan.Zeller29-Nov-12 1:36 
GeneralA bit harsh Pin
westville_mike16-Nov-09 2:50
westville_mike16-Nov-09 2:50 
GeneralDoes not respond on VB.Net 2008 Pin
KHALID SIBHAI7-Sep-09 3:13
KHALID SIBHAI7-Sep-09 3:13 
GeneralRe: Does not respond on VB.Net 2008 Pin
Gayan Buddhika7-Sep-09 17:34
professionalGayan Buddhika7-Sep-09 17:34 
GeneralRe: Does not respond on VB.Net 2008 Pin
KHALID SIBHAI7-Sep-09 23:09
KHALID SIBHAI7-Sep-09 23:09 
GeneralRe: Does not respond on VB.Net 2008 Pin
zpy_codeproject29-May-17 1:50
zpy_codeproject29-May-17 1:50 
GeneralTo all who criticise saying it bad Pin
Irwan Hassan27-Jun-09 2:32
Irwan Hassan27-Jun-09 2:32 
GeneralMy vote of 1 Pin
Jon_Boy3-Apr-09 8:24
Jon_Boy3-Apr-09 8:24 
GeneralConstructive criticism Pin
Bert delaVega3-Mar-08 6:07
Bert delaVega3-Mar-08 6:07 
GeneralUmmm, there is no reference to any database code whatsoever Pin
DelphiCoder29-Feb-08 13:33
DelphiCoder29-Feb-08 13:33 
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 Pin
Albert Dadze27-Feb-08 20:31
Albert Dadze27-Feb-08 20:31 
GeneralThis is one,.. Pin
Bert delaVega26-Feb-08 7:30
Bert delaVega26-Feb-08 7:30 

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

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