Search code examples
vb.netcomboboxscrollmousewheel.net-3.0

Disable mouse scroll wheel in combobox VB.NET


Does anyone know of a way to disable the mouse scroll wheel when a control such as a combobox or listbox has focus? For my purposes, combobox is all I need the answer for.

I have a combobox set to trigger a SQL query on SelectedIndexChanged, and accidentally scrolling the wheel while the combobox has focus causes about six SQL queries to fire off simultaneously.


Solution

  • The ComboBox control doesn't let you easily override behavior of the MouseWheel event. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.

    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
    

    Beware that this also disables the wheel in the dropdown list.