Context menu
-
When i click with the right mouse button shows the contextmenu. How to do this with the left mouse click.
C#
-
When i click with the right mouse button shows the contextmenu. How to do this with the left mouse click.
C#
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
-
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
But the contect menu appears on other location. I want it on the location where is the button.
C#
-
But the contect menu appears on other location. I want it on the location where is the button.
C#
-
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
Can someone tell me how to set the menu at the mouse position.
C#
-
Can someone tell me how to set the menu at the mouse position.
C#
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
-
Can someone tell me how to set the menu at the mouse position.
C#
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
-
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
Thanks for helping me.
C#
-
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
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
-
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
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 theControl.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
-
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 theControl.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