If you have the form's Type, it's pretty easy to do what you ask:
var type = typeof(MyForm);
var form = Activator.CreateInstance(type);
But what if you don't have the type? Well, you cannot directly instantiate a form from its title, its "name" (whatever that means), its menu item, or its URL. You will need some mechanism to map one of these back to the form's type.
It's possible some sort of mapping is available; for example, if the have the URL of a page, you might be able to map it to a handler using the ASP.NET framework. But there is no such mechanism that maps titles or menus to WinForm types.
If your database returns the type name you may be able to find the type like this:
public Form InstantiateFormFromTypeName(string typeName)
{
var type = System.Reflection.Assembly.GetExecutingAssembly()
.GetTypes()
.Where
(
t => t.FullName == typeName
&& typeof(Form).IsAssignableFrom(t)
)
.Single();
return Activator.CreateInstance(type) as Form;
}
...but you will need to be careful about scope and name. For example, there may be several types with the same name but in different namespaces or different assemblies.
If no automatic mapping is available, you can create your own, if you know the names of the forms ahead of time. If you store the mappings in a dictionary, for example, you can use it to create instances. Example:
//Define a dictionary where the key is the form name and the value
//is a function that will return a new instance of it.
var formList = new Dictionary<string, Func<Form>>
{
{ "Signon", () => new LoginForm() },
{ "Edit User", () => new EditUserForm() },
{ "Help", () => new FaqForm() }
};
//Use the dictionary to create a form
public Form InstantiateFormBasedOnName(string name)
{
return formList[name](); //Execute the function stored at index "name" in the dictionary
}