9

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

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

4

7 回答 7

15

我找到了一个混合响应,将此代码放入 MouseWheel 事件中:

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

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

于 2013-01-08T11:59:30.187 回答
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

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

于 2010-06-03T17:46:08.887 回答
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);
    }
}
于 2010-06-03T17:45:26.190 回答
0

一种这样的选择是将处理程序添加到组合框,并在该组合框内解决这种情况。我不确定您的代码是如何设置的,但我假设您是否知道事件何时发生,您可以设置某种条件来防止查询发生

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

通过这种方式,您将能够保持用户能够在组合框中滚动,还能够防止查询发生

于 2010-06-03T17:38:19.217 回答
0

结合此线程上的所有答案,如果您不想创建自定义控件,最好的解决方案是处理鼠标滚轮事件。如果下拉列表,下面还将允许滚动列表。

假设您的组合框称为组合框1:

If Not ComboBox1.DroppedDown Then
  Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)
  mwe.Handled = True
End If
于 2013-12-14T14:14:25.993 回答
0

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

于 2013-12-27T19:54:10.810 回答
0

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

于 2015-01-17T14:46:56.233 回答