Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have many Forms...and also I have a string that contain name of Form that must be Show...
for Example...I have TObjectForm and ApplianceForm
and string str = "ApplianceForm"
I need to create this form using this string that contain name of form that must be opened...
I use this...
C#
Type type = Type.GetType(typeName);
Form item = (Form)Activator.CreateInstance(type);


but it is error ArgumentNullException...but my string -> typeName is for example "ApplianceForm"...but it is error...
Posted

C#
string formTypeFullName = string.Format("{0}.{1}", this.GetType().Namespace, typeName);
Type type = Type.GetType(formTypeFullName, true);
Form item = (Form)Activator.CreateInstance(type);
item.ShowDialog();


type name need full name like this -> HouseDataBase.TObjectForm
 
Share this answer
 
Comments
Abhi261989 19-Nov-14 13:41pm    
thanx
You can use this:

C#
object oform;
oform = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("
[namespace].[formname]");
((Form)oform).Show();


replace the namespace with your application/form namespace and [formname] with the name of your form.
 
Share this answer
 
Comments
SilenthillpH 16-Feb-12 16:27pm    
still a NullReference Exception...oform is null...I don't know why ?
I use this..
Type type = Type.GetType(typeName, true);
Form item = (Form)Activator.CreateInstance(type);
item.ShowDialog();

and it is still error...it can't load this type
//Additional information: Could not load type 'TObjectForm' from assembly 'HouseDataBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
aidin Tajadod 16-Feb-12 16:38pm    
what about the code if posted. do you still getting error? are you entring the right NameSpace?
SilenthillpH 16-Feb-12 16:51pm    
no...stil error..
http://clip2net.com/s/1Bn1b
http://clip2net.com/s/1Bn1D
http://clip2net.com/s/1Bn1U
http://clip2net.com/s/1Bn2f
http://clip2net.com/s/1Bn2Y
http://clip2net.com/s/1Bn3t
http://clip2net.com/s/1Bn3G
....I don't know why.... ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900