How to tell what object is associate with context menu
-
Say I have two datagridviews (DG1, DG2) each with its own contextmenu, (Menu1,Menu2). Menu1 has menu items Save,Delete,Print and Menu2 has Save,Delete,Print now I would like to make one function that looks like private void Context_Menu_Save(object sender, EventArgs e){ DataGridView DG = (DataGridView)sender; } and then do what I need to do. The problem is that sender is the menuitem not the Datagridview that menuitem is associated with, so how do I find out which DataGridView's context menu was selected?
-
Say I have two datagridviews (DG1, DG2) each with its own contextmenu, (Menu1,Menu2). Menu1 has menu items Save,Delete,Print and Menu2 has Save,Delete,Print now I would like to make one function that looks like private void Context_Menu_Save(object sender, EventArgs e){ DataGridView DG = (DataGridView)sender; } and then do what I need to do. The problem is that sender is the menuitem not the Datagridview that menuitem is associated with, so how do I find out which DataGridView's context menu was selected?
Here are a couple of ways to overcome your problem: 1. the MenuItem belongs to some top-level menu, use its Parent property to find that top-level menu (the ContextMenu might be hierarchical); once you found the ContextMenu, that one has a SourceControl property which will point to the Control on which you right-clicked. 2. you could use the Tag property of the MenuItem and store something there, e.g. the underlying Control. 3. you could set up a dictionary that maps MenuItems to Controls. 4. you could organize a class member "lastDataGridViewClicked" and set it in the MouseDown handler of all DGVs. #1 requires no set-up effort, it does need a loop to cope with hierarchical menus though. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Say I have two datagridviews (DG1, DG2) each with its own contextmenu, (Menu1,Menu2). Menu1 has menu items Save,Delete,Print and Menu2 has Save,Delete,Print now I would like to make one function that looks like private void Context_Menu_Save(object sender, EventArgs e){ DataGridView DG = (DataGridView)sender; } and then do what I need to do. The problem is that sender is the menuitem not the Datagridview that menuitem is associated with, so how do I find out which DataGridView's context menu was selected?
Did you check the EventArgs? Sometimes they stuff things in like the OriginalSource, etc. Besides, how are you throwing up the context menu?
-
Did you check the EventArgs? Sometimes they stuff things in like the OriginalSource, etc. Besides, how are you throwing up the context menu?
Yes I looked in the eventargs didn't see anything in there either. And I'm throwing up the context menu via right click. (Not sure if that is what your asking) But I solved the problem by using the Tag property as suggested earlier.
-
Say I have two datagridviews (DG1, DG2) each with its own contextmenu, (Menu1,Menu2). Menu1 has menu items Save,Delete,Print and Menu2 has Save,Delete,Print now I would like to make one function that looks like private void Context_Menu_Save(object sender, EventArgs e){ DataGridView DG = (DataGridView)sender; } and then do what I need to do. The problem is that sender is the menuitem not the Datagridview that menuitem is associated with, so how do I find out which DataGridView's context menu was selected?
Try this:
ToolStripMenuItem itm = sender as ToolStripMenuItem; if (itm != null) { ContextMenuStrip cm = (ContextMenuStrip)itm.Owner; MessageBox.Show(cm.SourceControl.ToString()); }
Obviously I'm only showing the control in a
MessageBox
. You can do whatever you need to with it.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.” I wouldn't let CG touch my Abacus! When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is.