bavelt
bavelt

Reputation: 11

Add/modify handler in multiple ComboBoxes

I want to disable the mousewheel to prevent scrolling in the ComboBoxes.

For one ComboBox this works:

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    

But how can I add this to ALL ComboBoxes? There are a lot of them in the form.

I was looking for something like

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

Upvotes: 0

Views: 69

Answers (2)

bavelt
bavelt

Reputation: 11

Thanks!

It works. The problem I had was that the comboboxes are in several containers, such as Panels and Datagridviews. Then is "me.controls, etc" not enough.

So I finally made this out of it:

In the Form load:

EnumControls(Me)     

In the Programm:

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    

It works. Suggestions are welcome!

Upvotes: 1

Bas H
Bas H

Reputation: 2216

Try this

  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

and in FormLoad

 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

Upvotes: 0

Related Questions