Groups | Blog | Home
all groups > visual studio .net general > november 2004 >

visual studio .net general : How to dynamically create an instance of a form



Harshad Rathod
11/28/2004 4:07:05 PM
Ususally we do like:

SalesOrderForm fm = new SalesOrderForm();

where SalesOrderForm is a valid form in one of the assembly, that is
SalesOrderForm is a type. How can I create an instance of SalesOrderForm
when I do not know its type at run tyme? For example

public openForm (string formName)
{
// here I might get "SalesOrderForm" or something else. If I get
"SalesOrderForm" then I want to open it.
Here, it will also be NOT known which assembly this for is in.
}

Thanks.


Christoph Wienands
11/29/2004 4:02:49 PM
Hi Harshad,

[quoted text, click to view]

Runtime or design time? At runtime you'll have to know what and where, how
else would you be able to create the form.

[quoted text, click to view]

If the form name contains all the information about assembly name and form
name you can use reflection to create that type. Don't know out of my head
how to do that but reflection is the thing to look at. .Net assemblies can
be registered in the GAC with a strong name, alternatively you can make them
accessible through COM (automatic COM wrapper). Then you could create a form
of type "MyAssembly.MyForm".

Hope this helps,

Christoph

AddThis Social Bookmark Button