Open a new winform

I have a treeview, each node tag contains the name of the form, when I click on node, I open the form, my code looks like this

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { NodeClick(Convert.ToString(e.Node.Tag)); } public void NodeClick(string formName) { switch (formName) { case "frmPartMaster": 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(); } break; } } 

this code works fine, but I have 1000 forms, for each for the form I have to use the code correctly. is it possible if I submitted an open form, as in one case?

+4
source share
5 answers

You can instantiate a form class by calling Activator.CreateInstance

 public void OpenOrActivateForm(string formType) { var formType = Type.GetType(formType); var form = Globale.IsFormAlreadyOpen(formType); if(form == null) { form = Activator.CreateInstance(formType); from.Show(this); } else { form.Activate(); form.WindowState = FormWindowState.Normal; form.BringToFront(); } } 
+2
source

Why don't you just put the link to the form in the Node tag and then use it directly

 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { NodeClick(e.Node.Tag as Form); } public void NodeClick(Form theForm) { if(theForm == null) return; if(theForm.Visible == false) { theForm .Show(this); } theForm .Activate(); theForm .WindowState = FormWindowState.Normal; theForm .BringToFront(); } 
+2
source

You should be able to add a form to node, node should have a field, a tag, which I think is an object of type. Add your form, then extract it from the tag. This way you do not have to use the case statement, but one statement that works for all forms.

0
source

Instead of using a switch case for each form, you can use the Activator.CreateInstance method.

See the MSDN article .

You can save the Full Name in the tag and use it to create the appropriate form.

0
source

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.

0
source

All Articles