If the panel is hosted on a form,you could handle the form's MouseWheel event to scroll the panel like this:
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: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
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.Code:Public Class PanelWithWheelScrolling Inherits Panel Protected Overrides Sub OnMouseEnter(e As System.EventArgs) Me.Select() MyBase.OnMouseEnter(e) End Sub End Class
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:
At least the code is a bit simpler than my first version! BBCode: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
VB.Net程序设计:鼠标滑轮滚动Panel面板
发布时间:2020-12-17 08:06:03 所属栏目:百科 来源:网络整理
导读:http://www.vbforums.com/showthread.php?646751-Scrolling-a-panel-with-mousewheel Scrolling a panel with mousewheel. If the panel is hosted on a form,you could handle the form's MouseWheel event to scroll the panel like this: Code: Private Se
http://www.vbforums.com/showthread.php?646751-Scrolling-a-panel-with-mousewheel
Scrolling a panel with mousewheel.(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |