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. Context menu

Context menu

Scheduled Pinned Locked Moved C#
csharptutorial
11 Posts 3 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.
  • C Offline
    C Offline
    CodeItWell
    wrote on last edited by
    #1

    When i click with the right mouse button shows the contextmenu. How to do this with the left mouse click.

    C#

    M 1 Reply Last reply
    0
    • C CodeItWell

      When i click with the right mouse button shows the contextmenu. How to do this with the left mouse click.

      C#

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, Here I made a code example for the mouse down event on a button.

      	private void button1\_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
      	{
      		if(e.Button == MouseButtons.Left)
      		{
      			if(button1.ContextMenu!=null)
      			{
      				button1.ContextMenu.Show(button1, button1.Location);
      			}
      		}
      	}
      

      Hope it helps! All the best, Martin

      C 1 Reply Last reply
      0
      • M Martin 0

        Hello, Here I made a code example for the mouse down event on a button.

        	private void button1\_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        	{
        		if(e.Button == MouseButtons.Left)
        		{
        			if(button1.ContextMenu!=null)
        			{
        				button1.ContextMenu.Show(button1, button1.Location);
        			}
        		}
        	}
        

        Hope it helps! All the best, Martin

        C Offline
        C Offline
        CodeItWell
        wrote on last edited by
        #3

        But the contect menu appears on other location. I want it on the location where is the button.

        C#

        M 1 Reply Last reply
        0
        • C CodeItWell

          But the contect menu appears on other location. I want it on the location where is the button.

          C#

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Hello, This will set the menu starting location in the middle of the button. button1.ContextMenu.Show(button1, new System.Drawing.Point((button1.Width/2), (button1.Height/2))); All the best, Martin

          C 1 Reply Last reply
          0
          • M Martin 0

            Hello, This will set the menu starting location in the middle of the button. button1.ContextMenu.Show(button1, new System.Drawing.Point((button1.Width/2), (button1.Height/2))); All the best, Martin

            C Offline
            C Offline
            CodeItWell
            wrote on last edited by
            #5

            Can someone tell me how to set the menu at the mouse position.

            C#

            M S 2 Replies Last reply
            0
            • C CodeItWell

              Can someone tell me how to set the menu at the mouse position.

              C#

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello, You have to use the EventArgs properties "X" and "Y"!

              	private void button1\_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
              	{
              		if(e.Button == MouseButtons.Left)
              		{
              			if(button1.ContextMenu!=null)
              			{
              				button1.ContextMenu.Show(button1, new System.Drawing.Point(e.X, e.Y));
              			}
              		}
              	}
              

              All the best, Martin

              1 Reply Last reply
              0
              • C CodeItWell

                Can someone tell me how to set the menu at the mouse position.

                C#

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                button1.ContextMenu.Show(button1, Cursor.Current.Position);


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                C M 2 Replies Last reply
                0
                • S Stefan Troschuetz

                  button1.ContextMenu.Show(button1, Cursor.Current.Position);


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  C Offline
                  C Offline
                  CodeItWell
                  wrote on last edited by
                  #8

                  Thanks for helping me.

                  C#

                  1 Reply Last reply
                  0
                  • S Stefan Troschuetz

                    button1.ContextMenu.Show(button1, Cursor.Current.Position);


                    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                    www.troschuetz.de

                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #9

                    Hello Stefan,

                    Stefan Troschtz wrote:

                    button1.ContextMenu.Show(button1, Cursor.Current.Position);

                    I assume that this was out of your head without testing and in big hurry. ;) Because it's not compiling (at least not in framework1.1). I think what you meant was: button1.ContextMenu.Show(button1, Cursor.Position); But this is also not the right solution because the position of the contextmenu is relative to the button location. So you would have an offset depending on the position of the form. I think only the eventarg members X and Y are giving the right location, without an additional calculation of offsets. All the best, Martin

                    S 1 Reply Last reply
                    0
                    • M Martin 0

                      Hello Stefan,

                      Stefan Troschtz wrote:

                      button1.ContextMenu.Show(button1, Cursor.Current.Position);

                      I assume that this was out of your head without testing and in big hurry. ;) Because it's not compiling (at least not in framework1.1). I think what you meant was: button1.ContextMenu.Show(button1, Cursor.Position); But this is also not the right solution because the position of the contextmenu is relative to the button location. So you would have an offset depending on the position of the form. I think only the eventarg members X and Y are giving the right location, without an additional calculation of offsets. All the best, Martin

                      S Offline
                      S Offline
                      Stefan Troschuetz
                      wrote on last edited by
                      #10

                      Martin# wrote:

                      I assume that this was out of your head without testing

                      Almost true. Did not test, but got it from the docs whereby I oversaw that Position is a static property too.

                      Martin# wrote:

                      But this is also not the right solution because the position of the contextmenu is relative to the button location. So you would have an offset depending on the position of the form.

                      In fact, the Position property returns screen coordinates so unless the button is in the upper left region of the screen one probably cannot see the context menu. I must admit I didn't thoroughly read the documentation of the ContextMenu.Show method, so I missed that the point you're passing in has to be relativ to the location of the passed in control.

                      Martin# wrote:

                      I think only the eventarg members X and Y are giving the right location, without an additional calculation of offsets.

                      You're right. Using Cursor.Point would require an additional call to the Control.PointToClient method. Overall, a quite lousy post of mine :) Thanks for the addition. Regards, Stefan


                      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                      www.troschuetz.de

                      M 1 Reply Last reply
                      0
                      • S Stefan Troschuetz

                        Martin# wrote:

                        I assume that this was out of your head without testing

                        Almost true. Did not test, but got it from the docs whereby I oversaw that Position is a static property too.

                        Martin# wrote:

                        But this is also not the right solution because the position of the contextmenu is relative to the button location. So you would have an offset depending on the position of the form.

                        In fact, the Position property returns screen coordinates so unless the button is in the upper left region of the screen one probably cannot see the context menu. I must admit I didn't thoroughly read the documentation of the ContextMenu.Show method, so I missed that the point you're passing in has to be relativ to the location of the passed in control.

                        Martin# wrote:

                        I think only the eventarg members X and Y are giving the right location, without an additional calculation of offsets.

                        You're right. Using Cursor.Point would require an additional call to the Control.PointToClient method. Overall, a quite lousy post of mine :) Thanks for the addition. Regards, Stefan


                        "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                        www.troschuetz.de

                        M Offline
                        M Offline
                        Martin 0
                        wrote on last edited by
                        #11

                        Stefan Troschtz wrote:

                        Overall, a quite lousy post of mine

                        Don't be so hard with yourselve. All the best und noch einen schönen Tag! Martin

                        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