Mouse Event focus trouble

  • Thread starter Thread starter Luka
  • Start date Start date
L

Luka

Hi, I'm betting there is a trivial solution to this but I've spent
hours now trying to get it to work.

I have a panel to which I dynamically add controls at run-time. I want
the user to be able to draw lines between the controls in the
following manner:

1) MouseDown on ControlA and drag to ControlB which is connected on
MouseUp.
2) During the MouseMove a line to the current mouse position should be
drawn.
3) On the first MouseDown the selected control should be highlighted
4) On MouseEnter, the target control should be highlighted.

Problem: Once I start dragging (mousebutton + move), the first control
hogs the mouse events. Even when I do a MouseUp (or any other mouse
command for that matter) on another control, the first one responds.

I've tried just about everything I can think of, including forcing
focus/selection to the main panel but nothing has worked.

Any help would be greatly appreciated.
 
You should consider using .NET's built-in drag-and-drop support instead of
rolling your own with MouseDown/MouseMove/MouseUp. And what you're asking
for is a way to turn off mouse capture, which again is probably a bad idea
(imagine that the user presses the left mouse button, drags the mouse
somewhere outside the form, and releases the mouse button -- without mouse
capture, your app will never get notified that the mouse button was
released, so when they move the mouse back into your form, you'll still be
acting as if the mouse button was still pressed).

With those warnings in mind, to do what you're asking, you should just need
to look at the Control.Capture property. The edit box you click on will set
its Control.Capture to true as part of its mousedown handling, and you can
later set it back to false so that other controls will get the MouseMove and
MouseUp events.
 
Thanks, I did play with the Capture property yesterday but without any
success. I started from scratch on that part today and got it to work.

I'll stick to that principle and build in some safeguards should the
mouse move out of focus. The underlying panel which hosts the controls
already has a drag-drop handler that takes the same type of controls
so there's a risk of things getting very messy if I add an additional
drag-drop in the area.

Anyway, thanks - this was driving me insane :)
 
Luka,
In addition to Joe's comments.

Charles Petzold's book "Programming Microsoft Windows with Microsoft Visual
Basic" from MS Press has a lengthy discussion on mouse capture and its
subtleties. (there is a C# version of the book available).

Hope this helps
Jay
 
Back
Top