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

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
 
M

Michael Groeger

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
 
T

Truong Hong Thi

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.
 
L

Laurent

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
 

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