Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have a menu strip which is populated menu items dynamically.
i need to know which menu item is being clicked.?.
i am using the below code.

C#
MenuStrip menuStrip=new MenuStrip();

ToolStripMenuItem File = new ToolStripMenuItem();
ToolStripMenuItem Edit = new ToolStripMenuItem();
menuStrip.Items.Add(File);
menuStrip.Items.Add(Edit);

File.DropDown.Click += new System.EventHandler(this.FileMenuClick); 

private void FileMenuClick(object sender, EventArgs e)
        {
            MessageBox.Show("File Menu item clicked"); 
}


This code gives me that i just clicked a File menu item.
But i want to know which File menu item being clicked?..
Thanks in advance
Jim
Posted
Updated 12-Oct-13 1:52am
v2

1 solution

You can try this:

C#
private void SubmenuItem_Click(object sender, EventArgs e)
{
    var _targetMenuItem = sender as MenuItem; 
    var _mname = _targetMenuItem.Name;

    switch(_mname) 
     {
        case "File":
MessageBox.Show("File Menu item clicked");
           break;

        case "Edit":
MessageBox.Show("Edit Menu item clicked");
          break;
    
    }
}
 
Share this answer
 
v2
Comments
jim1972 12-Oct-13 8:08am    
tried this. but showing nullexception error.
abbaspirmoradi 12-Oct-13 8:12am    
add name for your items.
jim1972 12-Oct-13 8:15am    
when execution control reaches in this line, it breaks...
ys. i tried that only.

var menuText = _targetMenuItem.Name;
jim1972 12-Oct-13 8:24am    
i want to know which dropdown item have been clicked??..
abbaspirmoradi 12-Oct-13 8:32am    
add this event to all items and then try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900