views:

1110

answers:

3

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?

+3  A: 

Control.MousePosition and Control.MousButtons 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
Daniel Brückner
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.
danbruc, thanks for the heads up! I've edited my post to reflect your info.
Moose
+2  A: 

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
Ohhhh yes ... just forgot about the simplest thing ... :D
Daniel Brückner
Ohhhh no ... me will be null because the event arguments are not MouseEventArgs so it does not work.
Daniel Brückner
@danbruc - Uh, I just tried it to be sure... it works for me? Maybe I'm just holding my tongue right!
Moose
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
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
A: 

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.