How to open form by string name?

前端 未结 4 1341
盖世英雄少女心
盖世英雄少女心 2020-12-12 08:16

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\"
         


        
4条回答
  •  渐次进展
    2020-12-12 08:37

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

提交回复
热议问题