If i get string value from textbox and my Form name is same string value from textbox. How to open this form ?
string formAAA = textbox.text; // \"AAA\"
Use below:
private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
{
return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
}
private void Form1_Load(object sender, EventArgs e)
{
//Get all types
Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "loopClasses");
for (int i = 0; i < typelist.Length; i++)
{//Loop on them
if (typelist[i].BaseType == typeof(System.Windows.Forms.Form) && typelist[i].Name == textbox.text)
{//if windows form and the name is match
//Create Instance and show it
Form tmp =(Form) Activator.CreateInstance(typelist[i]);
//MessageBox.Show(typelist[i].Name);
tmp.Show();
}
}
}
You need to use reflection.
Opening a form is creating an instance of it, you need to create the instance and show it.
You are going to need the name of the form and its namespace.
string formName= textbox.Text;
string namespaceName = "MyNamespace.MyInternalNamespace";
Then you create your instance with activator.
var frm= Activator.CreateInstance(namespaceName, formName) as Form;
And then you just need to show the form
frm.show();
You can also use this:
Form GetFormByName(string name)
{
System.Reflection.Assembly myAssembly =System.Reflection.Assembly.GetExecutingAssembly();
foreach(Type type in myAssembly.GetTypes())
{
if (type.BaseType!=null&& type.BaseType.FullName == "System.Windows.Forms.Form")
{
if (type.FullName == name)
{
var form = Activator.CreateInstance(Type.GetType(name)) as Form;
return form;
}
}
}
return null;
}
string formtocall = "blabla";
var form = Activator.CreateInstance(Type.GetType("namespace." + formtocall)) as Form;
form.ShowDialog();