Open Form based on Treeview Choice

  • Thread starter Thread starter Paul Ilacqua
  • Start date Start date
P

Paul Ilacqua

I am using a treeview to group and display a list of my applications
functionality. How can I take the results of the treeview and open a form
based on a lookup value.
I pass the e.Node.FullPath value from the TreeView1_AfterSelect event

with .....
Dim iPos As Integer = e.Node.FullPath.LastIndexOf("\")
Dim sPath As String = e.Node.FullPath
SelectedText = sPath.Substring(iPos + 1, sPath.Length - iPos - 1)

.... and derive the selected text from that. From that point I lookup the
"SelectedText" value in the datatable to come up with the form's string
name.
IE
Dim f as new SelectedText
I hope I made my question clear.

Paul
 
... and derive the selected text from that. From that point I lookup the
"SelectedText" value in the  datatable to come up with the form's string
name.

If I understand the question correctly, you can use reflection to
lookup the type based off a string an create the form:

void Example()
{
OpenForm("WindowsFormsApplication1.Form2");
}

void OpenForm(string formName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type type = assembly.GetType(formName);
ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);

Form form = (Form)ci.Invoke(new object[0]);
form.Show();
}
 
When building the treeview, assign a reference to the appropriate Form to the
associated TreeNode. In the After_Select Event handler, retrieve the
reference:

Dim AForm as Form = e.node.Tag
AForm.ShowDialog
 
When building the treeview, assign a reference to the appropriate Form to the
associated TreeNode. In the After_Select Event handler, retrieve the
reference:

Dim AForm as Form = e.node.Tag
AForm.ShowDialog

That seems like it might be fairly inefficient if the number of nodes is
large. Using the tag property of the node is a good idea for figuring
out which form to create however. You could set the tag attribute to
"Form1" and create an instance of that through reflection in the
appropriate handler. This would be faster than parsing the path and
trying to figure out the form from the last path element.
 
Back
Top