bjswift
July 17th, 2007, 07:55 AM
I do not understand how to dynamically load a form. I have a list of choices the user can select, (from a treeview) and when the user clicks a node I want a form to load on the screen. How can I load a form dynamically?
I know how to:
dim frm as frmName
frm = new frmName
frm.show()
However, what if the frmName is not known until the user clicks one of the nodes in the treeview?
I want to:
dim frm as form
frm = new form(formNameFromTreeview)
frm.show() 'showing one of the forms clicked on.
Any help?
HanneSThEGreaT
July 17th, 2007, 09:13 AM
Hmm, not that easy nor that difficult. The main drawback you are facing is that there is no Forms Collection in .NET, so, that means you'd have to make your own.
Follow these steps.
Add a module, to your project, by selecting Project, Add Module...
Add this to your module :
Module FormsCollection
Public Forms As New FormsCollectionClass() 'New Collection object
Public frm1 As New Form1 'form1
Public frm2 As New Form2 'form2
Public frm3 As New Form3 'form3
End Module
Class FormsCollectionClass : Implements IEnumerable
Private c As New Collection() 'new collection
Sub Add(ByVal f As Form) 'add form to collection
c.Add(f)
End Sub
Sub Remove(ByVal f As Form) 'remove form from collection
Dim itemCount As Integer
For itemCount = 1 To c.Count
If f Is c.Item(itemCount) Then
c.Remove(itemCount)
Exit For
End If
Next
End Sub
ReadOnly Property Item(ByVal index) As Form 'get item
Get
Return c.Item(index)
End Get
End Property
Overridable Function GetEnumerator() As _
IEnumerator Implements IEnumerable.GetEnumerator
Return c.GetEnumerator 'enumerate
End Function
End Class
Then, follow these steps to add 2 more forms to your project:
Project, Add Form...
Now, we should have three forms ( just note here, in the module I only declared 3 form objects, because I'm dealing with 3 forms - demonstration purposes now only )
On Form 1, add a Treeview, and 2 buttons - keep all the default names.
Then, add the following code to Form 1 :
Public Class Form1
Private GetTreeText As String 'get selected from treeview
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Forms.Add(frm1) 'add form1 to collection
Forms.Add(frm2) 'add form2 to collection
Forms.Add(frm3) 'add form3 to collection
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As Form 'loop through our forms collection, and add each form name to the treeview
For Each frm In Forms
TreeView1.Nodes.Add(frm.Name)
Next
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Select Case GetTreeText 'determine selected treenode text
Case "Form1"
frm1.Show()
Case "Form2"
frm2.Show()
Case "Form3"
frm3.Show()
End Select
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
GetTreeText = TreeView1.SelectedNode.Text 'store selected item
End Sub
End Class
Once run, I click button 1, then it loads all the form names into the Treeview, when I have selected a Form name from the Treeview, and clicked on Button2, it will show the particular form that was selected.
I'm attaching a sample with :)
I hope it helps! :)
bjswift
July 17th, 2007, 09:47 AM
Thanks for the tip and the example. After posting my question, and going back to my search for a solution, I found this:
Dim objNewForm As Object = Activator.CreateInstance(Type.GetType("MyApplication." & formName))
Dim frm As Form = DirectCast(objNewForm, Form)
From there, I can open the form into a panel. It seems to work, and with less code, unless there are other reasons to keep a Forms collection?
Thanks again for your tip!