If the panel is hosted on a form,you could handle the form's MouseWheel event to scroll the panel like this:
Code:
Private Sensitivity As Integer = 20
Private Sub Panel1_MouseWheel(sender As Object,e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseWheel
If Panel1.Bounds.Contains(e.Location) Then
Dim vScrollPosition As Integer = Panel1.VerticalScroll.Value
vScrollPosition -= Math.Sign(e.Delta) * Sensitivity
vScrollPosition = Math.Max(0,vScrollPosition)
vScrollPosition = Math.Min(vScrollPosition,Me.VerticalScroll.Maximum)
Panel1.AutoScrollPosition = New Point(Panel1.AutoScrollPosition.X,_
vScrollPosition)
Panel1.Invalidate()
End If
End Sub
Edit: The panel's own MouseWheel event fires only when the panel has Focus. So another way is to make a control which inherits from Panel:
Code:
Public Class PanelWithWheelScrolling
Inherits Panel
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
Me.Select()
MyBase.OnMouseEnter(e)
End Sub
End Class
However,I haven't found a way yet to make the panel lose focus when the mouse leaves it,which could be a nuisance if you don't want it to scroll. Maybe someone else can think of a way.
Edit 2: An option is to pass the focus to a sibling control,but you should do it on the parent Form rather than in an inherited control as above:
Code:
Private Sub Panel1_MouseEnter(sender As Object,e As System.EventArgs) Handles Panel1.MouseEnter
Panel1.Select()
End Sub
Private Sub Panel1_MouseLeave(sender As Object,e As System.EventArgs) Handles Panel1.MouseLeave
Button1.Select()
End Sub
At least the code is a bit simpler than my first version! BB