how to add five dropdown menu item's event handler in c# dynamically

NazHim 201 Reputation points
2021-05-16T12:25:26.507+00:00

hi all
how to add five dropdown menu item's event handler in c# dynamically

dynamically added five dropdown menu items.
then how to add event handler for five menu items

with best regards
NazHim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,921 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Timon Yang-MSFT 9,571 Reputation points
    2021-05-17T01:51:33.257+00:00

    Are you using MenuStrip?

    After adding the item, we can see all the events in the "Properties" window, just double-click the event that needs to be added.

    97013-1.png

    We can also add events in the code like this:

            private void Form1_Load(object sender, EventArgs e)  
            {  
                MenuStrip thisMenuStrip = new MenuStrip();  
      
                ToolStripMenuItem thisFileItem = new ToolStripMenuItem("&File");  
                thisFileItem.DropDownItems.Add("&Clear");  
                thisFileItem.DropDownItems.Add("E&xit");  
      
                thisFileItem.DropDownItems[1].Click += Item_Click;  
      
                thisMenuStrip.Items.Add(thisFileItem);  
                this.Controls.Add(thisMenuStrip);  
                thisMenuStrip.Name = "menuStrip";  
                TabIndex = 0;  
            }  
      
            private void Item_Click(object sender, EventArgs e)  
            {  
                MessageBox.Show(sender.ToString());  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Convalle, Alessandro 0 Reputation points
    2023-11-13T12:39:26.4433333+00:00

    Thank you Timon this is the best solution to use.

    Ale

    0 comments No comments