By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,457 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,457 IT Pros & Developers. It's quick & easy.

Creating Windows Forms at runtime using System.Reflection

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
hi
i think this article on the MSDN can help a lot

http://msdn.microsoft.com/library/de...us/dnadvnet/ht
ml/vbnet10082002.asp
Hope it does
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2

P: n/a
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

"Bisbal" <Bi****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
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.

Nov 16 '05 #3

P: n/a
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

"Mohamoss" wrote:
hi
i think this article on the MSDN can help a lot

http://msdn.microsoft.com/library/de...us/dnadvnet/ht
ml/vbnet10082002.asp
Hope it does
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.