Creating Windows Forms at runtime using System.Reflection

G

Guest

Hi,

I'd like to know if it is possible to create a form type from a string. My
case: I have a DataTable with forms names (i.e frm_001_UserInfo.cs), and I'd
like to create an instance of that form from my code dynamically, retrieving
the form name from the database.

Something like str = "frm_001_UserInfo.cs" and then create an instance of
the form using str.

For what I've read about System.Reflection, there might be a way to do this,
but I dont know how.

Any help would be greatly appreciated.

Thanks in advance.
 
A

avnrao

u can use
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(formName)

and pass in your form name. but make sure that your form type exists in the
current assembly. If not, you have load the assembly that contains the form
and create an instance of that form.

hth
 
G

Guest

Mohamoss,

I use:

Dim GenericInstance As Object
GenericInstance = Activator.CreateInstance(TypeToLoad)

Dim FormToShow As Form = CType(GenericInstance, Form)

from the example to load a form that does not has parameters in its
constructor, everything work fine.

However, some of our forms need input parameters for the constructor, so I
use the following code:

Dim frm() As Form = {Me} ' Me is the calling form
Dim formActivator As Object = Activator.CreateInstance(formObject, frm)

I keep getting the following error message at the above line:

"An unhandled exception of type 'System.ArgumentNullException' occurred in
mscorlib.dll. Additional information: Value cannot be null."

Do you have any ideas what happen

Thanks

DavidT
 

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