Reflection issue

Cywizz

Centurion
Joined
Jan 31, 2003
Messages
136
Location
JHB, South Africa
Hi all
I've got a form name (string) that was read from a database field. I want to take this string and create a form instance and invoke the show/showdialog method on it.
Can anyone help me with this?

Thanks in advance :)
 
I don't understand. Do you mean you want to create a form that is
named whatever the string is? I don't think that's possible, considering
forms don't have Name properties anymore (their names are their
object names; there is no seperate string property like in VB6).

You weren't exactly clear about how the string ties in with the form
instance. :confused:
 
Let me clarify: I have a project that contains n amount of Forms. I store the class names (form names e.g. Form1 - nothing to do with the Name property) in a database. From a main form I would like to dynamicaly create a instance of one of these forms, using it's class name, and fire the Show/ShowDialog methods.
 
Last edited:
No worries, got the solution... if interested:
C#:
//In form1. if you want to create instance of form2 (the same assembly)
Type theObjectType = Type.GetType("YourNamespace.Form2");
Object theObject = Activator.CreateInstance(theObjectType);
//Get the method and invoke it
System.Reflection.MethodInfo m = theObjectType.GetMethod("Show");
m.Invoke(theObject,null);
 
Another, possibly better way:

C#:
Form myForm = (Form)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("YourNamespace.Form2");
myForm.Show();

This question has been asked on the forum before.
 
Thanks divil. This would be easier, if I knew the type and method - I also hold the object method in the database (Show or ShowDialog), so I'll need to call a method dynamically.
This project might progress to other data types, not just forms as well

Thanks again!
 
Back
Top