Try following code:
private void LoadNewForm(string formName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Form myForm = assembly.CreateInstance(formName) as Form;
myForm.Show(this);
}
[updates based on SA's suggestion]
Note that this will try to instantiate the form from the current assembly, which might not be the case. If you want to make some plugin feature (and you don't want to use standard plugin frameworks like
MEF or MAF[
^] for some reason), you can also load assembly at runtime. Please see:
Using Reflection to load unreferenced assemblies at runtime in C#[
^].
This works too, and you do not need to know the assembly.
private void LoadNewForm(string formName)
{
Type t = Type.GetType(formName);
Form frm = Activator.CreateInstance(t) as Form;
frm.Show(this);
}
You have to pass the fully qualified name of the form
type in both cases.
It is an other topic if you want to grab an existing instance of the form.