Disable mouse scroll wheel in combobox VB.NET
Asked Answered
F

7

9

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.

Field answered 3/6, 2010 at 17:8 Comment(0)
N
10

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.

Narah answered 3/6, 2010 at 17:46 Comment(3)
You can check the ComboBox's DroppedDown property and not set the EventArg's handled property if it is True. That will keep the scroll wheel working when the drop down list is shown.Threonine
If you want to pass the MouseWheel events to the ComboBox's parent control, after mwe.Handled = True you can call Parent.Focus (or Parent.Parent.Focus until you reach a control that can take focus, although this will get messy). When wrapped in an If DroppedDown = False, this works quite well when you don't want the ComboBox to interfere with a parent's ability to scroll.Piassava
This is not working, it seems it is never hit. Maybe an AddHandler missing?Erubescent
K
16

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.

Knipe answered 8/1, 2013 at 11:59 Comment(1)
Sweet answer. I used a shortened version: DirectCast(e, HandledMouseEventArgs).Handled = TrueMufi
N
10

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.

Narah answered 3/6, 2010 at 17:46 Comment(3)
You can check the ComboBox's DroppedDown property and not set the EventArg's handled property if it is True. That will keep the scroll wheel working when the drop down list is shown.Threonine
If you want to pass the MouseWheel events to the ComboBox's parent control, after mwe.Handled = True you can call Parent.Focus (or Parent.Parent.Focus until you reach a control that can take focus, although this will get messy). When wrapped in an If DroppedDown = False, this works quite well when you don't want the ComboBox to interfere with a parent's ability to scroll.Piassava
This is not working, it seems it is never hit. Maybe an AddHandler missing?Erubescent
S
1

If you subclass the control it's possible (apologies for the C#)

public class NoScrollCombo : ComboBox
{
    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    protected override void WndProc(ref Message m)
    {
        if (m.HWnd != this.Handle)
        {
            return;
        }

        if (m.Msg == 0x020A) // WM_MOUSEWHEEL
        {
           return;
        }

        base.WndProc(ref m);
    }
}
Stilted answered 3/6, 2010 at 17:45 Comment(1)
I am new to c#. can you please tell me where to use this to disable the scollbar on combobox. it is showing red scribbles to SecurityPermission.Orthopteran
S
0

One such option would be to add a handler to the comboBox, and within that comboBox, resolve the situation. I'm not sure how your code is set up, but I'm assuming if you knew when the event was happening, you could set up some kind of conditional to prevent the queries from happening

 '''Insert this statement where your form loads
 AddHandler comboBoxBeingWatched.MouseWheel, AddressOf buttonHandler

 Private Sub buttonHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
     '''Code to stop the event from happening
 End Sub

In this way, you'd be able to maintain the user being able to scroll in the comboBox, but also be able to prevent the queries from happening

Slippy answered 3/6, 2010 at 17:38 Comment(0)
C
0

Combining all the answers on this thread, the best solution if you don't want to create a custom control is to handle the mousewheel event. The below will also allow the list to be scrolled if it is dropped down.

Assuming your combobox is called combobox1:

If Not ComboBox1.DroppedDown Then
  Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
  mwe.Handled = True
End If
Chintzy answered 14/12, 2013 at 14:14 Comment(0)
C
0

I had the exact same issue, but found that simply changing the focus of the control after the query executed to another control such as the "Query" button itself worked better than perfect. It also allowed me to still scroll the control until the SelectedIndex actually changed and was only one line of code.

Calder answered 27/12, 2013 at 19:54 Comment(0)
C
0

Just put this in the mousewheel event or in a single handler for all the controls this applies to, maybe call it wheelsnubber. DirectCast(e, HandledMouseEventArgs).Handled = True

Caritacaritas answered 17/1, 2015 at 14:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.