How to open form by string name?

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

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

提交回复
热议问题