determine typeof new form dynamically in C#

  • Thread starter Thread starter Yoni Rabinovitch
  • Start date Start date
Y

Yoni Rabinovitch

I have an inheritance hierarchy of MDI child forms.

In the main MDIParent form, I have a splitter, with a TreeView on the
left side.
When clicking on a node in the tree, I want to be able to create a new
MDI child form, the Type of which is stored in the TreeNode.Tag
property.

My question is, is there a way I can determine the class of the new
form dynamically, instead of having a large "if else" with explicit
"new" statements for each kind of form I support ? Is there some way
to do this via a polymorphism ?
 
Yoni said:
My question is, is there a way I can determine the class of the new
form dynamically, instead of having a large "if else" with explicit
"new" statements for each kind of form I support ? Is there some way
to do this via a polymorphism ?

Search the net for examples on Activator.CreateInstance. That method
will allow you to create an object using a string which identifies the
object's type. Since all forms inherit from System.Windows.Forms.Form,
you can assign the created object to a variable of type Form and invoke
any of the methods exposed in the base class.
 
Back
Top