Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 716
Caleb Hearth Avatar asked Jun 03 '10 17:06

Caleb Hearth


2 Answers

I've found a mix response, put this code in the MouseWheel event:

Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
mwe.Handled = True

That's all. You don't need to create a new class, if you have your project in an advanced state.

like image 126
Jaime Bedmar Avatar answered Nov 11 '22 10:11

Jaime Bedmar


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.

like image 10
Hans Passant Avatar answered Nov 11 '22 11:11

Hans Passant