Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open form with Form Name in winform appliaction

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
  • GroupsManagement
  • LocationManagement

I get permission from database for these three forms

in menu click i fill tag Property with the name of form like this

tsmMain.Tag = item.PermissionName
tsmMain.Click += new EventHandler(tsmMain_Click);

what i want to do is to open form dynamically in button click and to remove these if condition? Can i do this with reflection or else??

ToolStripMenuItem aa = sender as ToolStripMenuItem;
        var tag = aa.Tag;
        if (tag == "User Management")
        {
            UserManagement oUserForm = new UserManagement();
            oUserForm.Show();
        }
        if (tag == "Groups Management")
        {
            GroupManagement oGroupForm = new GroupManagement();
            oGroupForm.Show();
        }
like image 387
DDR Avatar asked Dec 03 '25 06:12

DDR


1 Answers

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();
like image 112
Dave Bish Avatar answered Dec 04 '25 20:12

Dave Bish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!