UserControl and mouse scroll

jswota

Well-known member
Joined
Feb 23, 2009
Messages
49
Programming Experience
Beginner
Hi.

I have created a user control that displays CNC files. The control allows the user to zoom in and out with the mouse's scroll wheel. When i place the control on a form next to a grid control, the zooming no longer works. The mouse's scroll wheel causes the grid to scroll. Can someone please tell me how i can keep the usercontrol detecting the mouse's scroll?

Thanks.
 
I imagine it has to be with what control has the focus of the mouse. In form load try to make sure your usercontrol has the mouse focus.
 
If you click on the UC does it give the focus back to it and scroll/zoom correctly?
 
If you click on the UC does it give the focus back to it and scroll/zoom correctly?

No. The control has toolbars that allow you to zoom and pan also if you don't want to use your mouse. If i use the toolbars, everything works fine. I know my control has focus. If i immediately try to scroll, it goes right back to the grid.

Thanks for the help.
 
I am wondering if the mouse event is not wired up and it is picking your grid to scroll instead. Try adding an Addhandler to this control for the mouse event you are using and see if that solves it.
 
Try adding an Addhandler to this control for the mouse event you are using and see if that solves it.

Event handling is a bit foreign to me. Would you mind providing a sample of how to override the MouseWheel event? My zooming code is in a picturebox MouseWheel event as follows:

VB.NET:
Private Sub picCanvas_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseWheel
        If e.Delta < 0 Then
            Me.m_Canvas.Zoom(e_Canvas.enm_ZoomMode.zoom_Out)
        Else
            Me.m_Canvas.Zoom(e_Canvas.enm_ZoomMode.zoom_In)
        End If

        Me.picCanvas.Invalidate()
    End Sub

Thanks for your help.
 
So your saying it works on the form, but not when you place the grid control on the form, right?
 
Last edited:
So your saying it works on the form, but not when you place the grid control on the form, right?

Yes. I think it's because there is no other control to take focus. As soon as my usercontrol loses focus the scrolling will no longer work.

When i first run the app, the usercontrol has focus and zooms fine. When i move focus off the usercontrol and then back to the usercontrol the zooming no longer works. Everything else works. Panning for instance. The panning code is in the MouseMove event. That works fine!?! If i place a breakpoint in the MouseWheel event it never fires when scrolling on the usercontrol after i move focus. So it seems like i just have to get full focus back to the control when the user clicks on it.

Any suggestions?

Thanks again for all of your help.
 
Long Shot...

Try commenting out the mousewheel code in the usercontrol for now.
When you add the control and you get the 'Friend With Events <yourUserControl>...
VB.NET:
'Form_Load event
AddHandler <yourUserControl>.<yourControl>.MouseWheel, AddressOf picCanvas_MouseWheel
'Make this routed event on your form:
Private Sub picCanvas_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 'no handler needed here
     If e.Delta < 0 Then
        <yourUserControl>.Zoom(<yourUserControl>.enm_ZoomMode.zoom_Out)
     Else
        <yourUserControl>.Zoom(<yourUserControl>.enm_ZoomMode.zoom_In)
     End If
        <yourUserControl>.<yourControl>.Invalidate()
End Sub

That's if I got your class objects/properties right. I believe this will force this code to the foreground. Some events work fine for UC's when it is encapsulated in the control itself, but I have notice some work better like this.
 
From the above it seems your control doesn't take focus, this is especially noticable when you add some input control like a Textbox or similar and this automatically gets focus. Try to use the Select method in the controls MouseEnter event.
 
All,

What seems to be happening is that as soon as you place any object on the usercontrol and fill it's dock property the usercontrol itself cannot be clicked because the secondary object is in the way. If i take an empty usercontrol and drop it on a form the click event will fire when you click on it. If that usercontrol has secondary objects that "get in the way", you can't get the click event to fire - or many other events for that matter.

Anyone have any ideas how to handle this?

Thanks.
 
From the above it seems your control doesn't take focus, this is especially noticable when you add some input control like a Textbox or similar and this automatically gets focus. Try to use the Select method in the controls MouseEnter event.

The problem is the control's MouseEnter never fires. None of the mouse events and key events fire. The ScrollableControl that fills the UserControl is getting in the way. The ScrollableControl isn't designed to take focus.

I was able to work it out by adding:

ScrollableControl.Select to the ScrollableControl's Click event. Weird that i had to do this but it works.

Thanks for all of you help. I have another question about toolbar shortcuts on user controls that i will be posting shortly if you're interested.
 
Back
Top