Issue
- I have several menu items under the
View
menu in my parent form, each one of these menu items opens a child form. - I can call the child forms with a separate method for each menu item, but I want to combine all the menu items inside a switch case.
- How do I do that?
Separate event method for each child:
private void child1ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(Application.OpenForms["Child1"] is Child1 ch1)
{
ch1.Focus();
return;
}
ch1= new Child1();
ch1.Name = name1; //name assigned in parent form
ch1.Age = age1; // age assigned in parent form;
}
I have written the following code to combine the menu inside the switch statement, it isn't showing any error but does not display the child form either.
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
switch(menu.Name)
{
case "Child1":
if(Application.OpenForms["Child1"] is Child1 ch1)
{
ch1.Focus();
return;
}
ch1= new Child1();
ch1.Name = name1; //name assigned in parent form
ch1.Age = age1; // age assigned in parent form;
ch1.show();
break;
case "Child2":
if (Application.OpenForms["Child2"] is Child2 ch2)
{
ch2.Focus();
return;
}
ch2 = new Child2();
ch2.Name = name2; //name2 assigned in parent form
ch2.Age = age2; // age2 assigned in parent form;
ch2.show();
break;
case "Child3":
if (Application.OpenForms["Child3"] is Child3 ch3)
{
ch3.Focus();
return;
}
ch3 = new Child3();
ch3.Name = name3; //name3 assigned in parent form
ch3.Age = age3; // age3 assigned in parent form
ch3.show();
break;
default:
break;
}
}
Solution
okay I solved it, if anyone is looking for the same problem, the solution is in the Parent.Designer.cs window where you find auto-generated code for the controls.
this.chil1ToolStripMenuItem.Name = "chil1ToolStripMenuItem"
so the menu.name
in Parent.cs should be the same as the one above name or you can edit the this.chil1ToolStripMenuItem.Name = "chil1ToolStripMenuItem"
to
this.chil1ToolStripMenuItem.Name = "chil1Menu"
Answered By - Lha Answer Checked By - Gilberto Lyons (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.