I have a canvas with a mouse up and down event and there are normaly around 10-15 clickable objects on the screen.
If I click down on an element or blank space I can catch the event, but when I release the mouse it its only caught if the mouse wasn't on another element (blank space is fine).
Is there any way in Silverlight (for WP7) to make sure I get a mouse up event no matter where on the screen it its triggered?
You should simply Capture the mouse when you get the mousDown event.
(And release it after your mouseUp )
Related
I wonder if D&D events not always firing (DragLeave/DragEnter).
I implemented a D&D feature in my WPF GUI. While dragging some element around I 'dragleave' a GUI element. Normally it fires an appropriate 'DragLeave' event, but not always. I fear, that for some speed reasons sometimes these events get not fired reliable. If that's the case, how can I overcome these issue?
Short Version:
I wrote some sample app to check it myself - and yes - the DragLeave event is not fired reliable.
Long Version:
What I did:
I placed a Label as a draggable object inside a StackPanel. Then I implemented the code for the MouseMove and DragLeave events. An additional TextBlock - added to the StackPanel as well - served as event output. If the DragLeave handler gets called, I changed the Text Property of the TextBlock to 'DragLeave occured'. Afterwards I tested it dragging the Label towards the StackPanel.
What I recognized:
If I was fast enough or pressed the mouse button close to the border of the Label, the DragLeave event was not fired. If I did it slow, everything worked fine as expected.
What I conclude:
I guess, if the mouse leaves the Label before the 'DoDragDrop' thread is initiated, the event is not fired, since the mouse is already outside the Label. So there is a small glitch between starting the D&D and the firing of the first event.
One can see the same behavior in the DragOver/MouseMove handler. The faster you move the mouse, the fewer points (e.GetPosition(...)) you can capture in the event handler.
Problem is, that for the DragLeave that missing event can be critical, since it is only fired once. However, the DragOver event is fired very often during a dragging and a missing event can be balanced by the next DragOver event directly afterwards.
I have a combobox control's previewmousewheel event been handled. When my mouse is on the control and i move the center wheel of my mouse then this event gets raised. But when my mouse is away from the control and i move the center wheel of my mouse then this event does not get raised.
Can anybody please explain me why is this happening?
If I want to raise an event when my mouse is away from the control then which event should I handle?
This is expected behaviour.
If the mouse cursor is not positioned over the control then no mouse events are routed through it. You wouldn't expect a mouse click event to be routed through it if the cursor was over a different control would you?
If you want this behaviour then I would suggest that you handle the mousewheel event in the page/view and route it from there, be cautious though as user expectation is for mouse and keyboard events to be handled by the in focus item.
In those cases that the user would expect the event to be forwarded to the control you could use Mouse.Capture(someControl) and Mouse.Capture(null) to stop the forwarding.
You should only use this when it makes sense. E.g. when dragging a scrollbar, when you started to drag on the thumb but are not required to stay on top of it as long as you keep the left mouse button down.
When using Mouse.Capture() make sure you always provide a way back from capturing.
i have two richtextboxes one below the other in my application.when the user start selection in one richtextbox and continue to the other richtextbox selection should automatically move to the second richtextbox.is there any way to do this type of selection.
thanks in advance,
dhyanesh
You'd think you could use MouseEnter and MouseLeave, but when the mouse is captured (as it is during text selection), these events don't fire as expected.
The way to achieve your goal is:
Subscribe to MouseMove on the first RichTextBox.
In the MouseMove event, check Mouse.Captured to see if it is the RichTextBox.
If the mouse is captured, do a hit test on the mouse's current postion using VisualTreeHelper.HitTest. Go up the visual tree from the value of HitTestResult.VisualHit to see if the mouse is over a RichTextBox other than the current one.
If the mouse is over a new RichTextBox, cancel the mouse capture with Mouse.Capture(null), then fire a MouseLeftButtonDown event on the new RichTextBox to cause it to capture the mouse and begin selection.
I have a borderless window (BorderStyle = None) where I would like to allow the user to click on the background (anywhere but one of the child controls) and move it around.
How would I go about doing that?
Thank you,
Try this.
http://www.codeproject.com/KB/cs/csharpmovewindow.aspx
Just be sure and add a mouse event handler immediately after the form is initialized.
You just need to override OnMouseDown/OnMouseMove/OnMouseUp. These three methods provide the mouse events to move your form.
When the mouse is pressed, just track the mouse location. On move events, move the form with the mouse, and when the mouse is released, stop "moving".
I've created a WPF application where the title bar and chrome are turned off. I have a border around the entire app, with the idea that it would act like the chrome in some regards. The first thing I'm trying to do is have the mousemove event capture the movement of the mouse when the mouse is clicked, so that the window moves with the mouse. The problem is that if the mouse moves too quickly, it manages to leave the window and therefore the mousemove no longer fires. I've been able to do this with a normal WinForm with no problems regardless of the speed of the mouse. Is there any way to do this more efficiently, or perhaps tune the polling of the mousemove? Perhaps a different container other than border that would perform better?
Try Me.DragMove in the window's left click event handler. It much better than most custom solutions.
When the user clicks you should capture the mouse (see Mouse.Capture). That way, you'll get the mouse events regardless of whether the mouse cursor is over your element or not.