8

Is there a method to obtain the (x, y) coordinates of the mouse cursor in a controls DoubleClick event?

As far as I can tell, the position has to be obtained from the global:

Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y

Also, is there a method to obtain which button produced the double click?

Aaron Wagner
  • 5,739
  • 1
  • 31
  • 38
user79755
  • 2,623
  • 5
  • 30
  • 36
  • the weird thing is that while debugging, you can actually see the value in there but no amount of casting will give you access to it. – John Lord Nov 26 '22 at 20:46

3 Answers3

15

Use the MouseDoubleClick event rather than the DoubleClick event. MouseDoubleClick provides MouseEventArgs rather than the plain EventArgs. This goes for "MouseClick" rather than "Click" as well...and all the other events that deal with the mouse.

MouseDoubleClick makes sure the mouse is really there. DoubleClick might be caused be something else and the mouse coordinates may not be useful - MSDN: "DoubleClick events are logically higher-level events of a control. They may be raised by other user actions, such as shortcut key combinations."

Community
  • 1
  • 1
user410352
  • 151
  • 1
  • 2
12

Note: As danbruc pointed out, this won't work on a UserControl, because e is not a MouseEventArgs. Also note that not all controls will even give you a DoubleClick event - for example, a Button will just send you two Click events.

  private void Form1_DoubleClick(object sender, EventArgs e)
   {
       MouseEventArgs me = e as MouseEventArgs;

       MouseButtons buttonPushed = me.Button;
       int xPos = me.X;
       int yPos = me.Y;
   }

Gets x,y relative to the form..

Also has the left or right button in MouseEventArgs.

Moose
  • 5,354
  • 3
  • 33
  • 46
  • Ohhhh yes ... just forgot about the simplest thing ... :D – Daniel Brückner Mar 27 '09 at 18:27
  • Ohhhh no ... me will be null because the event arguments are not MouseEventArgs so it does not work. – Daniel Brückner Mar 27 '09 at 18:35
  • @danbruc - Uh, I just tried it to be sure... it works for me? Maybe I'm just holding my tongue right! – Moose Mar 27 '09 at 18:36
  • I just tried it for a UserControl and it does not work; getting null. May be some controls actualy pass MouseEventArgs to the handler but you should not rely on that, at least do a not null check and future version might break this behavior. For what control(s) does it work? – Daniel Brückner Mar 27 '09 at 18:45
  • I missed the "control's doubleclick"... I was doing it on the form. I will edit this to reflect my goof. thanks for the heads up. – Moose Mar 27 '09 at 18:49
6

Control.MousePosition and Control.MouseButtons is what you are looking for. Use Control.PointToClient() and Control.PointToScreen() to convert between screen and control relative coordinates.

See MSDN Control.MouseButtons Property, Control.MousePosition Property, Control.PointToClient Method, and Control.PointToScreen Method for details.


UPDATE

Not to see the wood for the trees... :D See Moose's answer and have a look at the event arguments.

This MSDN article lists which mouse actions trigger which events depending on the control.

UPDATE

I missed Moose's cast so this will not work. You have to use the static Control properties from inside Control.DoubleClick(). Because the button information is encoded as bit field yoou have to test as follows using your desired button.

(Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left
Community
  • 1
  • 1
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
  • How would I determine which button is pressed using Control.MouseButtons? It contains constants to compare to, but other than that I don't see how to use it. The doubleclick eventargs does not contain a variable to compare with this. – user79755 Mar 27 '09 at 18:26
  • danbruc, thanks for the heads up! I've edited my post to reflect your info. – Moose Mar 27 '09 at 18:51
  • Your answer is better: there's no MouseDoubleClick event in the Compact Framework, but your method is fine. – Soonts Jun 24 '15 at 21:05