How to create a form form its class name stored in a string

  • Thread starter Thread starter Laurent
  • Start date Start date
L

Laurent

Hi All,

I would like to create a form dynamically from its class name stored in a
string.

For ex. in pseudo-code:

class MyForm : Form
{
.....
}

class UseForm
{
Form CreateTheForm()
{
return something-I-dont-known("MyForm");
}
}

Who has a solution ?

Laurent
 
Hi Laurent,

you can try Activator.CreateInstance or System.Assembly.CreateInstance to do
so:

Form f = Activator.CreateInstance(null, "MyForm") as Form; // null means
that the type "MyForm" must be in the executing assembly

Regards,
Michael
 
Try some overload versions of Activator.CreateInstance.
e.g.
return (Form) Activator.CreateInstance("MyFormAssembly",
"MyNameSpace.MyForm").UnWrap();
If same assembly, pass null for the first param.
 
Thanks a lot

Michael Groeger said:
Hi Laurent,

you can try Activator.CreateInstance or System.Assembly.CreateInstance to
do
so:

Form f = Activator.CreateInstance(null, "MyForm") as Form; // null means
that the type "MyForm" must be in the executing assembly

Regards,
Michael
 
Back
Top