Developer
Developer

Reputation: 8646

Loading a Form with out its instance

Hi all I would like to get the form loaded from the database, I am storing my form name as Form1 in my DB. Now I would like to get this form name on the other form button click event I tried as follows by storing the form name in a string but I am unable to load the form can some one help me

string strFromName="Form1";
Type type = Type.GetType(strFromName); // Null reference is coming here
Form form = (Form)Activator.CreateInstance(type);

Upvotes: 0

Views: 55

Answers (2)

PHeiberg
PHeiberg

Reputation: 29851

You have to supply the fully qualified name of the form ("TopNamespace.SubNameSpace.Form1") or if the form class is residing in an Assembly other than the calling you have to supply an AssemblyQualifiedName ("TopNamespace.SubNameSpace.Form1,MyAssembly").

See the Type.GetType documentation for the details.

Upvotes: 1

burning_LEGION
burning_LEGION

Reputation: 13460

Get all types from assembly and compare instances with your string "Form1" by name after you can get instatnce type

Upvotes: 1

Related Questions