Sensitive border area between MouseEnter and MouseLeave events - c#

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.

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.

Strange white area in panel c#

I have a problem with panel and weird area on it. I fill my panel with many PictureBoxes 32x32px, and a small area of this panel is filled with white area.
Here is how it looks like:
You can see that the first PictureBox has specified grass image, which is 32x32px, but the PictureBox below has only half of it's image. It's very strange.
I have also an onClick event specified for PictureBoxes to change it's background to other image. If I click on 'working' PictureBox it's background changes, but when I click on 'corrupted' one, it doesn't.
So basically, my question is - what could be the reason for such effect? Is it possible to find it out without analysing a code? I would like to avoid putting a code here, because it's very complicated and long.
EDIT
I used WinSpy++ and it is the result (red point is a place where i hover cursor)
so we can see that PictureBox is partly hidden behind this white area.
I don't know if the question is still active, but I'll try to respond anyway. It is more a comment on it, but since I'm not allowed to make comments yet I'll just answer.
I had encountered a similar issue when implementing some picutreBox drawing using onPaint event handler. The problem was that I called pictureBox.Invalidate() during onPaint and it caused displaying of the unwanted white color box. You might want to avoid using Invalidate() or Refresh() in your onPaint event if there is one.
If this is not the case it might also help to refresh the form or pictureBoxes that are corrupted. Try to call this.Refresh() after the form initialization, preferably in onLoad or onShown event handler.
If it still doesn't help then the problem is somewhere else, I'd guess there is a control hidden somewhere that causes that. But we'll need to see some code in order to suggest any other advice.

Resizing without changing the controls

I am sure many developers are frustrated by the flickering when resize events happens faster than onPaint event.
I have created many user objects that is shown on the main form.
When I try to set the location and size of each controls at the resize event of the main form, I get the flickering.
To resolve this I wanted to do a new approach as mentioned below.
When resizeBegin event is triggered, show a borderline of current frame
When resize event is triggered, only the borderline will be resized leaving the controls in the main frame untouched.
The tricky part is when the borderline is larger than original size, I want it to show the background of the window as it self.
Click for Enlarge image
On the contrary if the borderline is smaller then, I still want the original objects to be shown
Click for Reduce image
When resizeEnd then the borderline will be hidden and the controls in the frame will be resized.
Does anybody know how to implement the tricky part mentioned at resize?

MouseEnter and MouseLeave Event not raising in user control

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.

In WPF, how can I capture mouse on a Canvas and still find what controls the cursor is hovering over?

I have a custom Canvas control (inherited from Canvas) overlaid over a large area of User Controls. The idea is to draw paths between user controls (i.e. connector lines).
To capture mouse movement, I call Mouse.Capture(theCanvas) on MouseDown. This works beautifully, but the user controls under the canvas obviously no longer receive mouse events. Mouse.DirectlyOver always shows the canvas, so I can't really fake it by peeking at the current position and seeing which user control it's over.
So, I still need the Canvas for drawing paths, but how can I solve this one of the following ways:
Peek under the Canvas and see what the topmost control is right under it?
Get this MouseDown -> Track MouseMoves -> MouseUp workflow to work on the canvas without mouse captures?
Any other ideas welcome...
I'd agree that those are your two options. If you want to only forward some clicks to your usercontrols, then go with option 1, and hit test the controls under the canvas.
If you need your usercontrols to behave as though there is nothing covering them (textboxes, buttons etc), then i'd recommend using the PreviewMouseMove event on the user control's parent, as this can pick up and optionally "handle" events before the controls get at the event, but it won't block the event if you don't set handled to true

Categories

Resources