【问题标题】:Add/modify handler in multiple ComboBoxes在多个 ComboBox 中添加/修改处理程序
【发布时间】:2021-07-11 15:56:12
【问题描述】:

我想禁用鼠标滚轮以防止在组合框中滚动。

对于一个 ComboBox 这是有效的:

Private Sub CmbDienst_MouseWheel(sender As Object, e As MouseEventArgs) Handles CmbDienst.MouseWheel
  Dim HMEA As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
 HMEA.Handled = True
End Sub    

但是如何将它添加到所有组合框?表格中有很多。

我正在寻找类似的东西

 Private Sub Combo_Mouse()
    For Each c As Control In Me.Controls.OfType(Of ComboBox)()
        'And then...?
    Next
 End Sub    

【问题讨论】:

  • 您可以使用相同的处理程序使您的所有 ComboBox 控件订阅该事件。或者构建一个自定义控件并覆盖 OnMouseWheel。然后用您自己的替换所有标准控件(需要处理)。

标签: vb.net combobox


【解决方案1】:

谢谢!

它有效。我遇到的问题是组合框位于多个容器中,例如 Panels 和 Datagridviews。那么“me.controls, etc”还不够吗。

所以我终于成功了:

在表单加载中:

EnumControls(Me)     

在程序中:

Private Sub ComboBoxes_MouseWheel(sender As Object, e As MouseEventArgs)
    Dim hmea = DirectCast(e, HandledMouseEventArgs)
    hmea.Handled = True
End Sub

Private Sub EnumControls(ByVal ctl As Control)
    If ctl.HasChildren Then
        For Each c As Control In ctl.Controls
            For Each comboBox In c.Controls.OfType(Of ComboBox)()
                AddHandler comboBox.MouseWheel, AddressOf ComboBoxes_MouseWheel
            Next
            EnumControls(c)
        Next
    End If
End Sub    

它有效。欢迎提出建议!

【讨论】:

    【解决方案2】:

    试试这个

      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
    

    在表单加载中

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each c As Control In Me.Controls.OfType(Of ComboBox)()
            'You cann acces to ComboBox her by c
            AddHandler c.MouseWheel, AddressOf buttonHandler
        Next
    End Sub
    

    【讨论】:

    • 因为我是新来的,所以我看到顺序是错误的。我对答案的回复置于成员的答案之上。我想我做错了什么......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2012-04-15
    相关资源
    最近更新 更多