Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to tell what object is associate with context menu

How to tell what object is associate with context menu

Scheduled Pinned Locked Moved C#
questionhelptutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2254591
    wrote on last edited by
    #1

    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?

    L S H 3 Replies Last reply
    0
    • U User 2254591

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • U User 2254591

        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?

        S Offline
        S Offline
        SledgeHammer01
        wrote on last edited by
        #3

        Did you check the EventArgs? Sometimes they stuff things in like the OriginalSource, etc. Besides, how are you throwing up the context menu?

        U 1 Reply Last reply
        0
        • S SledgeHammer01

          Did you check the EventArgs? Sometimes they stuff things in like the OriginalSource, etc. Besides, how are you throwing up the context menu?

          U Offline
          U Offline
          User 2254591
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • U User 2254591

            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?

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups