How to open form by string name?

前端 未结 4 1331
盖世英雄少女心
盖世英雄少女心 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:50

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

提交回复
热议问题