Winform 屏蔽ComboBox控件鼠标滚轮滚动

方法1,直接在MouseWheel事件中写代码,缺点,每个控件都需要写代码才行.
Private Sub cboMacGroup_MouseWheel(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles cboMacGroup.MouseWheel
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
方法2, 用addHandler为MouseWheel绑定一个处理函数,可以多个控件共用同一个函数,但仍然是每个控件都需要AddHandler 后才生效
AddHandler comboBoxBeingWatched.MouseWheel, AddressOf buttonHandler
Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub
方法3: 建一个类,继承ComboBox控件,重写OnMouseWheel函数, 然后项目中全部使用MyComboBox,对于新建的窗体用这个方法最好,但如果项目已经完成,需要逐个替换,也是有点麻烦。
Friend Class MyComboBox
Inherits ComboBox
Protected Overrides Sub OnMouseWheel(ByVal e As MouseEventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
End Sub支脚
End Class垃圾焚烧
方法4:类似方法3,新建类继承ComboBox,重写WndProc方法,屏蔽鼠标滑轮滚动消息
双生筷
c#
public class NoScrollCombo : ComboBox
{
[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
protected override void WndProc(ref Message m)
{
if (m.HWnd != this.Handle)
{
return;
}
if (m.Msg == 0x020A) // WM_MOUSEWHEEL
{
return;
}
base.WndProc(ref m);
}
}
vb
Public Class NoScrollCombo
Inherits ComboBox
<SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode := True)>
Protected Overrides Sub WndProc(ByRef m As Message)
If m.HWnd <> Me.Handle Then
Return
End If
If m.Msg = &H20A Then ' WM_MOUSEWHEEL
Return
End If
MyBase.WndProc(m)
End Sub
End Class
方法5:
最后说一下DataGridView中使用ComboBox列时的屏蔽方法:
甘汞电极
涨紧轮Private Sub dgItems_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgItems.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
Dim cbo As ComboBox = e.Control
AddHandler cbo.MouseWheel, AddressOf disableMouseWheel
End If
End Sub
Private Sub disableMouseWheel(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True
双蛋合体End Sub 
上面提供一些基本思路,可以在此基础上再写一个通用的函数,在窗体内执行一次就行。
思路是遍历窗体所有控件,如果是ComboBox则用addHandler绑定上
面的disableMouseWheel函数,
如果是DataGridView则用addHandler为EditingControlShowing事件绑定一个函数,类似方法5

本文发布于:2024-09-23 11:27:10,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/287070.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:控件   方法   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议