在combobox VB.NET中禁用鼠标滚轮

时间:2010-06-03 17:08:44

标签: vb.net combobox scroll mousewheel .net-3.0

当组合框或列表框等控件具有焦点时,是否有人知道禁用鼠标滚轮的方法?就我的目的而言,我需要的是组合框。

我有一个组合框设置为在SelectedIndexChanged上触发SQL查询,并且在组合框具有焦点时意外滚动滚轮导致大约六个SQL查询同时触发。

7 个答案:

答案 0 :(得分:14)

我发现了混合响应,将此代码放在MouseWheel事件中:

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

这就是全部。如果您的项目处于高级状态,则无需创建新类。

答案 1 :(得分:10)

ComboBox控件不允许您轻松覆盖MouseWheel事件的行为。在项目中添加一个新类并粘贴下面显示的代码。编译。将新控件从工具箱顶部拖放到表单上。

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

请注意,这也会禁用下拉列表中的滚轮。

答案 2 :(得分:1)

如果您将控件子类化,则可能(为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);
    }
}

答案 3 :(得分:0)

一个这样的选项是向comboBox添加一个处理程序,并在该comboBox中解决这种情况。我不确定你的代码是如何设置的,但我假设如果你知道事件何时发生,你可以设置某种条件以防止查询发生

 '''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

通过这种方式,您可以维护用户能够在comboBox中滚动,但也能够阻止查询发生

答案 4 :(得分:0)

结合此线程的所有答案,如果您不想创建自定义控件,最佳解决方案是处理mousewheel事件。如果列表被删除,下面也将允许滚动列表。

假设你的组合框叫做combobox1:

If Not ComboBox1.DroppedDown Then
  Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
  mwe.Handled = True
End If

答案 5 :(得分:0)

我有完全相同的问题,但发现只需将查询执行后的控件焦点更改为另一个控件(如“查询”按钮本身)就比完美更好。它还允许我仍然滚动控件,直到SelectedIndex实际改变,只有一行代码。

答案 6 :(得分:0)

只需将它放在鼠标滚轮事件中,或者在适用于所有控件的单个处理程序中,也可以将其称为wheelnubber。 DirectCast(e,HandledMouseEventArgs).Handled = True