Newbie Q: How to determine WHICH item was clicked on context menu

R

Rex

Hi All - In the following method (the click event of my Context Menu
control):

private void contextMenuStrip1_MouseClick(object sender,
MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("left click");
}
}

the message "left click" successfully appears. What I can't figure out
(and this might be obvious!) is how to determine WHICH of the 5 menu
items that are found on my Context Menu that the User clicked.

Thanks,
Rex
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Rex said:
Hi All - In the following method (the click event of my Context Menu
control):

private void contextMenuStrip1_MouseClick(object sender,
MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("left click");
}
}

the message "left click" successfully appears. What I can't figure out
(and this might be obvious!) is how to determine WHICH of the 5 menu
items that are found on my Context Menu that the User clicked.

Thanks,
Rex

It might be possible if you use the mouse coordinates to find out where
the mouse is in relation to the menu items, but that's not normally how
it's done.

Use the click events of the separate menu items instead.
 
C

Carl Daniel [VC++ MVP]

Rex said:
Hi All - In the following method (the click event of my Context Menu
control):

private void contextMenuStrip1_MouseClick(object sender,
MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("left click");
}
}

the message "left click" successfully appears. What I can't figure out
(and this might be obvious!) is how to determine WHICH of the 5 menu
items that are found on my Context Menu that the User clicked.

Typically you'll wire up a handler to the Click (not MouseClick) event on
each menu item and route each menu item to it's own handler. That way you
don't have to figure anything out.

-cd
 
R

Rex

ok... that definitely helps - I need to take a different approach!
Now can anyone point me toward some information on HOW to set that up?
I would really appreciate it.
Rex
 
M

Marc Gravell

If using the designer, then just double-click on the item you want. If
coding from scratch, then just
{your menu item}.Click += {your handler}

Alternatively, both ToolStripItemCollection and MenuItemCollection
supply an Add() overload that accept an EventHandler in the method.

If you want to share the handlers, then you might be able to cast the
sender:

void MySharedHandler(object sender, EventArgs args) {
MenuItem clicked = sender as MenuItem;
if(clicked!=null) {
// this one!
}
}

Marc
 
R

Rex3444

Thank you, Marc, and thank you, Carl and Goran.
Marc, you supplied the final missing piece for me.
For the benefit of others, here's what I did:

1. I added a "contextMenuStrip" control to my form (leaving the
default Name of "contextMenuStrip1").

2. For the control that the User is going to right-click on, I set the
value of its "ContextMenuStrip" property to "contextMenuStrip1".

3. I double-clicked on the "Opening" Event in the Properties window of
contextMenuStrip1.
Within that "private void contextMenuStrip1_Opening(object sender,
CancelEventArgs e)" method, I did the following:

contextMenuStrip1.Items.Clear();

contextMenuStrip1.Items.Add("my item 1");
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Enabled =
true;
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Click +=
new EventHandler(mDoRightClickAction);

contextMenuStrip1.Items.Add("my item 2");
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Enabled =
true;
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Click +=
new EventHandler(mDoRightClickAction);

4. I created a method called mDoRightClickAction, as follows:

private void mDoRightClickAction(object sender, System.EventArgs e)
{
MessageBox.Show(sender.ToString());
}

That MessageBox.Show will display either "my item 1" or "my item 2",
depending on which item the User clicks on.

Thanks again, everyone,

Rex

=====================================================
=====================================================
=====================================================
 
B

blasferatu

Thank you, Marc, and thank you, Carl and Goran.
Marc, you supplied the final missing piece for me.
For the benefit of others, here's what I did:

1. I added a "contextMenuStrip" control to my form (leaving the
default Name of "contextMenuStrip1").

2. For the control that the User is going to right-click on, I set the
value of its "ContextMenuStrip" property to "contextMenuStrip1".

3. I double-clickedon the "Opening" Event in the Properties window of
contextMenuStrip1.
Within that "private void contextMenuStrip1_Opening(object sender,
CancelEventArgs e)" method, I did the following:

contextMenuStrip1.Items.Clear();

contextMenuStrip1.Items.Add("myitem1");
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Enabled =
true;
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Click +=
new EventHandler(mDoRightClickAction);

contextMenuStrip1.Items.Add("myitem2");
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Enabled =
true;
contextMenuStrip1.Items[contextMenuStrip1.Items.Count - 1].Click +=
new EventHandler(mDoRightClickAction);

4. I created a method called mDoRightClickAction, as follows:

private void mDoRightClickAction(object sender, System.EventArgs e)
{
MessageBox.Show(sender.ToString());
}

That MessageBox.Show will display either "myitem1" or "myitem2",
depending on whichitemthe User clicks on.

Thanks again, everyone,

Rex

=====================================================
=====================================================
=====================================================

Hi All - In the following method (the click event of my Context Menu
control):
private void contextMenuStrip1_MouseClick(object sender,
MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("left click");
}
}
the message "left click" successfully appears. What I can't figure out
(and this might be obvious!) is how to determine WHICH of the 5 menu
items that are found on my Context Menu that the Userclicked.
Thanks,
Rex- Ocultar texto citado -

- Mostrar texto citado -

Hi.
I handle the ContextMenuStrip_ItemClicked event and them use the
e.ClickedItem.Name to determine which item has been clicked
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top