PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] How do I combine the menu items inside the swicth case in C#

 November 07, 2022     .net-framework-version, c#, menu, switch-statement, winforms     No comments   

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?

Menu Image

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing