You are on page 1of 3

30/12/2022 15:48 c# - How to open form by string name?

- Stack Overflow

How to open form by string name?


Asked 6 years, 7 months ago Modified 3 years, 6 months ago Viewed 7k times

If i get string value from textbox and my Form name is same string value from textbox. How to
open this form ?
-2
string formAAA = textbox.text; // "AAA"

I need to open form 'AAA';

c# winforms

Share Follow asked May 30, 2016 at 10:06


โทนี่ สตาร์ค
9 1 1

Sorted by:
4 Answers
Highest score (default)

string formtocall = "blabla";

7 var form = Activator.CreateInstance(Type.GetType("namespace." + formtocall))


as Form;

form.ShowDialog();

Share Follow answered May 30, 2016 at 10:09


Hüseyin Burak Karadag
1,810 16 22

2 If you think this answered your question then please mark it as the answer to help others find the
correct answer quickly and also give Hüseyin the credits. – MrApnea May 30, 2016 at 13:51

You need to use reflection.

0 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";
Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.

https://stackoverflow.com/questions/37522945/how-to-open-form-by-string-name 1/3
30/12/2022 15:48 c# - How to open form by string name? - Stack Overflow

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();

Share Follow answered May 30, 2016 at 10:11


Aimnox
875 7 20

Use below:

0 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();
}
}

Share Follow answered May 30, 2016 at 10:14


Sufyan Jabr
751 4 20

You can also use this:

0 Form GetFormByName(string name)


{
System.Reflection.Assembly myAssembly
=System.Reflection.Assembly.GetExecutingAssembly();
foreach
Join Stack Overflow (Type
to find thetype
best in myAssembly.GetTypes())
answer to your technical question, help others
{ Sign up
answer theirs. if (type.BaseType!=null&& type.BaseType.FullName ==
https://stackoverflow.com/questions/37522945/how-to-open-form-by-string-name 2/3
30/12/2022 15:48 c# - How to open form by string name? - Stack Overflow
"System.Windows.Forms.Form")
{
if (type.FullName == name)
{
var form = Activator.CreateInstance(Type.GetType(name)) as
Form;
return form;
}
}
}
return null;

Share Follow answered Jun 4, 2019 at 21:03


Joaquin Lpz B
1

Join Stack Overflow to find the best answer to your technical question, help others
Sign up
answer theirs.

https://stackoverflow.com/questions/37522945/how-to-open-form-by-string-name 3/3

You might also like