You can use this approach:
Define a dictionary of lines and actions like this
Dictionary<string, Action> dic = new Dictionary<string,Action>(); dic.Add("frmPartMaster", OpenPartMaster); .....
add appropriate action
private void OpenPartMaster() { frmPartMaster partMaster = null; if ((partMaster = (frmPartMaster)Globale.IsFormAlreadyOpen(typeof(frmPartMaster))) == null) { partMaster = new frmPartMaster(); partMaster.Show(this); } else { partMaster.Activate(); partMaster.WindowState = FormWindowState.Normal; partMaster.BringToFront(); } }
and when you need to call this form instead of an infinite switch, use
dic[formName].Invoke();
this way you have a centralized point where you add a specific action to execute when a specific form is requested and you retain all the written functionality.
Of course, you need to reorganize the switch enclosures using separate methods.
This approach is interesting if you have different actions (cases) for your form, and not always the same sequence of repeating codes.
Steve source share