Open form with Form Name in winform appliaction

前端 未结 2 667
猫巷女王i
猫巷女王i 2020-12-19 21:58

I want to ask what should i do to open form with the help or class name in winform c#?

I have three different forms

  • UserManagement
  • GroupsManag
相关标签:
2条回答
  • 2020-12-19 22:43

    You may be able to do something like this, using the name of your form, as a string argument:

    var form = (Form)Activator.CreateInstance(Type.GetType("YourNameSpace.UserManagement"));
    form.Show();
    
    0 讨论(0)
  • 2020-12-19 22:55

    One straightforward, but not necessarily very clean solution would be to store the forms right there in the Tag property of your menu items, rather than the strings.

    Somewhere at the beginning of your application, you'd have to assign these instances:

    myUserManagementItem.Tag = new UserManagement();
    myGroupsManagementItem.Tag = new GroupManagement();
    

    Then, in the click event, you could shorten your code to:

    ToolStripMenuItem aa = sender as ToolStripMenuItem;
    Form form = aa.Tag as Form;
    form.Show();
    

    Cleaner solutions would include the following:

    • Provide separate event handlers for different menu items.
    • Derive your own menu item types that store the form to show in a strongly-typed property.
    0 讨论(0)
提交回复
热议问题