I'm using c# and I have a MenuStrip control but I don't know how to identify what item of it is being clicked. For example, I used to group all click(buttons) events in one or two methods like "btnActions_click()" or "btnNavigation_click()". Then, inside of the method I identify the button clicked by parsing the sender as a button and placing it on a button var, then I check if the name of that button var is equal to "btnFoo" or "btnBar".
So, in this case, how could I know what item of the MenuStrip controls is being clicked in order to group all click events in only one method?
I apologyze if my english isn't correct. If you can't understand me I can try again or post some code.
Thanks.
Edit: I didn't post any code because I thought that there was not necessary in this question but someone suggest me to do it, so I'll do it. Here's an example of what I do to identify what button has been clicked.
private void btnNavegation_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn.Name == "btnNext")
//go to next item of the list
else if (btn.Name == "btnPrevious")
//go to previous item of the list
}
I think that you need to subscribe to ItemClicked event (inherited from ToolStrip
), instead of subscribing to Click event (inherited from Control
).
The example provided by Microsoft's documentation show you how to determine the clicked item on each call (ToolStripItemClickedEventArgs::ClickedItem
):
private void ToolStrip1_ItemClicked(Object sender, ToolStripItemClickedEventArgs e)
{
System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "ClickedItem", e.ClickedItem );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "ItemClicked Event" );
}