vb.net中通过继承 ComboBox控件类,如何禁用鼠标滚轮mousewheel事件或重写Mousewheel事件使Mousewheel无效
时间: 2024-11-22 07:38:55 浏览: 48
在VB.NET中,如果你想要继承ComboBox控件并且禁用或重写其`MouseWheel`事件,你可以按照以下步骤操作:
1. 首先,创建一个新的类,让它继承自`System.Windows.Forms.ComboBox`类。例如:
```vbnet
Public Class CustomComboBox Inherits ComboBox
```
2. 然后,在新类中,你可以重写`OnMouseWheel`事件处理方法,并设置其为`Nothing`或`Throw New NotImplementedException()`,这将使该事件默认失效。这样做是为了阻止事件的传递:
```vbnet
Protected Overrides Sub OnMouseWheel(e As MouseEventArgs)
' 或者直接设置为 Nothing
RaiseEvent MouseWheel(Me, e)
End Sub
```
或者
```vbnet
Protected Overrides Sub OnMouseWheel(e As MouseEventArgs)
Throw New NotImplementedException()
End Sub
```
3. 如果你想完全禁止这个事件,可以覆盖`CanRaiseEvents`属性,将其设置为`False`,使得事件无法被触发:
```vbnet
Protected Overrides ReadOnly Property CanRaiseEvents As Boolean
Get
Return False
End Get
End Property
```
4. 使用你的`CustomComboBox`实例替代原生的`ComboBox`即可。
阅读全文
相关推荐


















