How to Load Windows Form From Data Base






1.19/5 (14 votes)
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