views:

2152

answers:

4

I have a usercontrol I created. I added a panel and a vertical scrollbar to the right of it. I'd like to be able to scroll it with the mousewheel. The problem is there doesn't seem to be any events that fire on mousewheel. If I take the panel off then the usercontrol has focus and it will fire on mousewheel in the form. But with the panel on it doesn't seem to fire the mousewheel event of the panel, or the usercontrol within the control or even on the form. The best solution would be to have an event fire in the usercontrol but I'd even accept an event on the form and feed it back into the usercontrol.

I'm using vb.net and vs2005 if it matters.

A: 

Here is an article about working with mouse events in vb.net. It specifically mentions mouse scroll.

notandy
+1  A: 

Do this -

  • Create a new VB.NET Winforms project
  • Place a Panel control on the form
  • Set the "AutoScroll" property of the panel to "true"
  • Place the following code in the load event of the form

        For i As Integer = 1 To 100
            Dim b As New Button()
            b.Text = i.ToString()
            b.Size = New Size(60, 40)
            b.Location = New Point(0, (i * b.Height) - b.Height)
            b.Parent = Panel1
            Panel1.Controls.Add(b)
        Next
  • Run the project.

You should see a form with 100 buttons inside the panel control. The panel control should contain a vertical scroll bar. Using the scroll wheel inside the panel should scroll through the buttons.

Hope this example helps.

Edit

I added a panel and a vertical scrollbar to the right of it.

That is not the correct way to do it. You need to use the autoscroll property of the panel.

Edit - Another Example

  • Create a new VB.NET project
  • Place two buttons on the form
  • Create a new usercontrol
  • Set the autoscroll property of the usercontrol to true
  • Paste the following code in the form load event

        Dim uc As New UserControl1
        uc.Parent = Me
        Me.Controls.Add(uc)
        uc.Size = New Size(100, 100)
        uc.Location = New Point(0, 0)
        For i As Integer = 1 To 100
            Dim b As New Button()
            b.Text = i.ToString()
            b.Size = New Size(60, 40)
            b.Location = New Point(0, (i * b.Height) - b.Height)
            b.Parent = uc
            uc.Controls.Add(b)
        Next
  • Run the program. Click the buttons (on the form). Notice, that you have to click the usercontrol to set its focus and use the scroll wheel.
Thanks, setting up that example helped me figure out a workaround. My problem though is entirely from the use of a usercontrol. Doing the panel on a form works.
dwidel
Do you need to use a panel? A usercontrol *is* a panel. Setting the autoscroll of the usercontrol should be sufficient. By default, you *do* need to click the usercontrol (or any control for that matter) to give it focus. Using a separate panel and a separate scrollbar is probably not necessary.
...continued... Of course I don't know the entire context of your usercontrol.
+1  A: 

I've been researching this all day, I may have figured this out. The mousewheel event is only sent to the control with focus. A panel can't have focus. Since the panel is covering the usercontrol it can't get the focus either. (unless it's the only control on the form) If on the panel mouseenter event I call me.focus it sets the focus to the usercontrol allowing it to receive the mousewheel event. The event fires in the form and the control both. I'm still open to suggestions if there's a better way though, as this seems a little hacky.

dwidel
+2  A: 

yes, a panel can have focus. You just have to give it focus, I prefer to use on mouse over.

I did this and its problem solved.

Neil N