MouseEnter and MouseLeave Event not raising in user control - c#

Here is the problem:
I have a simple c# form
I have a simple c# user control containing a picturebox and other button.
My form contains one instance of the user control.
I want that when the user do a mouseEnter in the picture box, the mouse cursor change and when the user do a mouseLeave of the picturebox, the mouse go back to normal.
What is happening now is that the events are not fired at all. I put break point into MouseOver, MouseEnter, MouseMove, MouseLeave, etc and none of thems fired. It's the first time I have this problem in C#.
I think it has something to do with the "routed event" but I can't figure it out. If there is another way to achieve what I'm doing, this will also be considered a solution. What is important is that at the end, the user control will be the master of the mouse cursor over his "territory".
Thanks in advance!

What events are you using? The UserController.MouseEnter and UserController.MouseLeave events or the PictureBox.MouseEnter and PictureBox.MouseLeave events?
You should use the latter as the PictureBox will handle the event if the mouse enters the user controller directly through the PictureBox.

As InBetween wrote, PictureBox.MouseXXX should be firing. You can trap those in your UserControl.
If you want the event to be fired on behalf of UserControl, just disable the PictureBox. Be aware though that the event would fire for any mouse position over the UserContrl, not only the PictureBox.

Related

Picturebox blocks MouseEnter event from firing?

as a part of my WinForms application, I want to move a picturebox (blue box) onto the grid as seen below.
I'm moving the picturebox according to the mouse positions (while the left mouse button is pressed down).
Now my problem is, that when dragging the picturebox over the grid, the MouseEnter event is supposed to be called (each box in the grid is a picturebox in itself, with the event attached to it). The event gets called when simply moving with the mouse over it, but as soon as I'm dragging the blue box over it, it doesn't.
So my question is, is it possible that the picturebox on top (blue box) blocks the MouseEnter event of the picturebox on the bottom from firing? And if so, is there a way to work around it? I've thought about using Drag&Drop, but this seemed as the easier solution.
Thanks in advance.

Sensitive border area between MouseEnter and MouseLeave events

I have a PictureBox control on my form for which I have written two events for MouseEnter and MouseLeave. On MouseEnter anther PictureBox enters the form and stands beside the original and with MouseLeave the second PictureBox goes way.
All works fine. Except when the cursor is on the original PictureBox’s border area the MouseEnter and MouseLeave events are repeatedly run. So the second image enters and leaves the form until the cursor is taken away. This makes a strange sight.
How can I avoid this situation?
The border area can be tricky, especially when you want to trigger something the might influence it even if it is only by a few pixels..
One classic situation is when you want to resize or move a control by clicking an dragging it at its border. Unless you use the internal calls and simply code mouseenter, -leave, -move, -down and -up you may well end up with e.g. moving the control away from the mouse and thereby triggering another leave event.
This often occurs only at one set of borders, like left&top or right&bottom.
You need to check you code for any such influences, like the new PictureBox pushing the old one away by a few pixels or a resize that makes it smaller; even one pixel can lead to the effect you see..
If the MouseEnter event is triggering a border to be drawn, or the size of the first PictureBox to change in any way, that can cause the effect you describe.
You could add a check against the mouse coordinates in MouseEnter to ensure that the mouse pointer gets far enough into the control's interior before the event fires. That would prevent an instant firing of the Leave event.

OnMouseMove() event handler delegation

I'm trying to write to a status bar the current mouse position and in order to that I took over OnMouseMove() event handler which is triggered when the mouse cursor enters my control. The problem is that in my control I have a WPF control which is has Dock.Fill Dockstyle, meaning, it fills the entire parent control.
When I run the applicaiton I see that nothing happens and the mouse position isn't updated, so I've noticed that the OnMouseMove() event of the WPF control is triggered and not the OnMouseMove() event of my control, which contains the relevant code for updating the mouse location coordinates.
I wanted to know if there's a way (other than implementing the code in the WPF OnMouseMove() event handler, of course) to bypass the WPF control event handler and use always my control's event handler.
I hope I was clear enough in my question, if not please let me know and I'll try to elaborate.
Thanks!
try the PreviewMouseMove-Version of OnMouseMove - this one should fire correct
and you need to have some kind of background (not null) or your controll won't see the mouse - so give it a transparent color or something)

c# Control.Validating event leaves mouse focus on other controls?

I have a c# .net 2.0 winForm with a textbox and a trackbar. The textbox Validating event sets e.cancel if the user clicks the trackbar and the validation fails. I am then left with the cursor in the textbox, but the mouse focus is still on the trackbar so moving the mouse moves the trackbar.
I have tried SetFocus in the validating event (bad according to MSDN but I tried anyway) but the mouse stays on the trackbar.
How do I detach the mouse focus from the trackbar?
Are you displaying the validation error message in a message box. If that is the case the mouse release of trackbar wouldnt have fired as the message box would have taken control and that's the reason you are seeing the trackbar moving after you exit the message box.
MessageBox and validating event dont go well with each other. Best way to do it is to use a ErrorProvider.

C# MouseClick Event a bit laggy, how can I fix this?

I'm making a craps game, and I have a "Craps Table" image in a PictureBox Control.
I've made a MouseClick event handler for the PictureBox to check what region was selected by the user, and add a bet to that "Part of the Craps Table" when clicked.
Everything works great, except if I click the region very quickly, the event fires only once for every two clicks (Approx).
I've searched everywhere, and not quite sure what I'm doing wrong. I thought at first it might be a graphics problem, but I've ruled just about everything out, and I'm thinking that the event simply isn't firing properly at high click speeds, I have to pause for half a second between clicks for it to fire.
Thanks for any advice..
Also guessing, Try MouseDown instead of MouseClick...
Another thing to check: If your app is high-cpu intensive, and the UI thread is mostly busy, the mouse events will arrive late.
My guess is that by clicking very quickly, you're triggering the doubleclick event instead of click

Categories

Resources