Calling forms dynamically

M

Mac

Hi all,

I have a treeview in a MDI container form which is going to act as a menu for
my windows application. When someone clicks on a node in the treeview it will
open a new form.

I know I could write an exhaustive case statement that handled each node
individually but I would to do this with as minimal code as possible. The
first thing I was going to do was create a custom treeview control and add a
property for the form name. Therefore when I click on a node I will have the
form I want to open.

But I need to create an instance of this form before I show it. So is it
possible to do this dynamically such that I could use the same chunk code to
instaniate all possible forms that are in the treeview and then show them?

regards,

Mac
 
H

Herfried K. Wagner [MVP]

Mac said:
I have a treeview in a MDI container form which is going to act as a menu
for
my windows application. When someone clicks on a node in the treeview it
will
open a new form.

I know I could write an exhaustive case statement that handled each node
individually but I would to do this with as minimal code as possible. The
first thing I was going to do was create a custom treeview control and add
a
property for the form name. Therefore when I click on a node I will have
the
form I want to open.

But I need to create an instance of this form before I show it. So is it
possible to do this dynamically such that I could use the same chunk code
to
instaniate all possible forms that are in the treeview and then show them?

Instantiate the forms and assign them to the treenodes' 'Tag' properties.
Later you can show the form as shown below:

\\\
DirectCast(Me.TreeView1.SelectedNode.Tag, Form).Show()
///

Alternatively you can create the form instances by need, which is
preferrable when dealing with lots of forms. To do that, assign the form
types to the treenodes' 'Tag' properties:

\\\
Dim tn As New TreeNode()
....
tn.Tag = GetType(FooForm)
....
///

Creating a form using a type object:

\\\
Imports System.Reflection
..
..
..
Dim frm As Form = _
DirectCast( _
Activator.CreateInstance( _
DirectCast(Me.TreeView1.SelectedNode.Tag, Type) _
), _
Form _
)
frm.Show()
///
 
M

Mac via DotNetMonster.com

Thanks to Cor, Eric & Herfried for postings on this issue - you have given me
plenty to look into.

regards,

Mac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top