Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine what control the ContextMenuStrip was used on

I have a ContextMenuStrip that is assigned to several different listboxes. I am trying to figure out when the ContextMenuStrip is clicked what ListBox it was used on. I tried the code below as a start but it is not working. The sender has the correct value, but when I try to assign it to the menuSubmitted it is null.

private void MenuViewDetails_Click(object sender, EventArgs e) {     ContextMenu menuSubmitted = sender as ContextMenu;     if (menuSubmitted != null)     {         Control sourceControl = menuSubmitted.SourceControl;     } } 

Any help would be great. Thanks.

Using the assistance below, I figured it out:

private void MenuViewDetails_Click(object sender, EventArgs e)         {             ToolStripMenuItem menuItem = sender as ToolStripMenuItem;             if (menuItem != null)             {                 ContextMenuStrip calendarMenu = menuItem.Owner as ContextMenuStrip;                  if (calendarMenu != null)                 {                     Control controlSelected = calendarMenu.SourceControl;                 }             }         } 
like image 724
Taryn Avatar asked Feb 03 '11 12:02

Taryn


People also ask

Which property we have to set to use the ContextMenuStrip?

The Items property is used to add and work with items in a ContextMenuStrip.

How do I use ContextMenuStrip?

Drag and drop or double click on a ControlMenuStrip control to add it to the form. Add the menu items, Cut, Copy and Paste to it. Add a RichTextBox control on the form. Set the ContextMenuStrip property of the rich text box to ContextMenuStrip1 using the properties window.

Which of the following represents a shortcut menu a ContextMenuStrip B ContextMenuStrip C ContextMenuStrip D ContextMenuStrip?

ContextMenuStrip replaces ContextMenu. You can associate a ContextMenuStrip with any control, and a right mouse click automatically displays the shortcut menu.


2 Answers

For a ContextMenu:

The problem is that the sender parameter points to the item on the context menu that was clicked, not the context menu itself.

It's a simple fix, though, because each MenuItem exposes a GetContextMenu method that will tell you which ContextMenu contains that menu item.

Change your code to the following:

private void MenuViewDetails_Click(object sender, EventArgs e) {     // Try to cast the sender to a MenuItem     MenuItem menuItem = sender as MenuItem;     if (menuItem != null)     {         // Retrieve the ContextMenu that contains this MenuItem         ContextMenu menu = menuItem.GetContextMenu();          // Get the control that is displaying this context menu         Control sourceControl = menu.SourceControl;     } } 

For a ContextMenuStrip:

It does change things slightly if you use a ContextMenuStrip instead of a ContextMenu. The two controls are not related to one another, and an instance of one cannot be casted to an instance of the other.

As before, the item that was clicked is still returned in the sender parameter, so you will have to determine the ContextMenuStrip that owns this individual menu item. You do that with the Owner property. Finally, you'll use the SourceControl property to determine which control is displaying the context menu.

Modify your code like so:

private void MenuViewDetails_Click(object sender, EventArgs e) {      // Try to cast the sender to a ToolStripItem      ToolStripItem menuItem = sender as ToolStripItem;      if (menuItem != null)      {         // Retrieve the ContextMenuStrip that owns this ToolStripItem         ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;         if (owner != null)         {            // Get the control that is displaying this context menu            Control sourceControl = owner.SourceControl;         }      }  } 
like image 104
Cody Gray Avatar answered Oct 06 '22 00:10

Cody Gray


I had great difficulty getting any of this code to work. This is the simplest solution I could find:

For A ContextMenuStrip:

    Control _sourceControl = null;     private void contextMenuStrip_Opened(object sender, EventArgs e)     {         _sourceControl = contextMenuStrip.SourceControl;     }      private void contextMenuItem_Click(object sender, EventArgs e)     {         var menuItem = (ToolStripMenuItem)sender;          _sourceControl.Text = menuItem.Text;         MessageBox.Show(menuItem.Name);         MessageBox.Show(sourceControl.Name);     } 
like image 35
Nick Allan Avatar answered Oct 05 '22 22:10

Nick Allan