Open Form in C# knowing Name Only

  • Thread starter Thread starter vb6dev
  • Start date Start date
V

vb6dev

Hi,

I asked a question a few days ago that wasn't clear enough. I need to open
a form in a C# project, but I know only its name.
I guess I should look into Reflexions to do this, but it doesn't work for me
yet. I am using the following code:

string assemblyName = "WindowsApplication1";
string typeName = "WindowsApplication1.Test";
object o = Activator.CreateInstance(assemblyName, typeName, null);
((Form)o).Show();

I have a System.TypeLoadException in mscorlib.dll on line 3. What I am
missing there?

Thanks

vbdev
 
If this code is running from within WindowsApplication1, I would do the
following instead:

object o =
Assembly.GetExecutingAssembly().CreateInstance("WindowsApplication1.Form1");
((Form) o).Show();

If you are using remoting in any way please disregard my response, this code
is only running good locally.
 
Hello,
Try using
object o = Activator.CreateInstance(typeof(WindowsApplication1.Test));
((Form)o).Show();

HTH, Cheers.
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
Back
Top