Click here to Skip to main content
Licence CPOL
First Posted 25 Feb 2008
Views 21,282
Bookmarked 26 times

How to Load Windows Form From Data Base

How to Load Windows Form From Data Base
13 votes, 76.5%
1

2
1 vote, 5.9%
3
2 votes, 11.8%
4
1 vote, 5.9%
5
1.19/5 - 17 votes
3 removed
μ 1.45, σa 2.38 [?]

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)

Sri Lanka Sri Lanka

Member
G.A Pubudu Gayan Buddhika currently working as a Senior Software Engineer in AmSafe Bridport.This year He will complete His Masters Degree. Pubudu has more than 5 year industries experience as a Software Developer and system Designer. He has experience in VB 6.0 VB.NET 2003 AND 2005. also He has experience developing Active X controls.

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_mike3:50 16 Nov '09  
GeneralDoes not respond on VB.Net 2008 PinmemberMember 16763914:13 7 Sep '09  
GeneralRe: Does not respond on VB.Net 2008 PinmemberGayan Buddhika18:34 7 Sep '09  
GeneralRe: Does not respond on VB.Net 2008 PinmemberMember 16763910:09 8 Sep '09  
GeneralTo all who criticise saying it bad PinmemberIrwan Hassan3:32 27 Jun '09  
GeneralMy vote of 1 PinmemberJon_Boy9:24 3 Apr '09  
GeneralConstructive criticism PinmemberBert delaVega7:07 3 Mar '08  
GeneralUmmm, there is no reference to any database code whatsoever PinmemberDelphiCoder14:33 29 Feb '08  
GeneralSurprisingly irrelevant PinmemberAlbert Dadze21:31 27 Feb '08  
GeneralThis is one,.. PinmemberBert delaVega8: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.120210.1 | Last Updated 2 Mar 2008
Article Copyright 2008 by Gayan Buddhika
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid