在组合框 VB.NET 中禁用鼠标滚轮

发布于 2024-09-04 21:13:30 字数 145 浏览 11 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

韵柒 2024-09-11 21:13:31

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

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

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.

开始看清了 2024-09-11 21:13:31

如果您对控件进行子类化,这是可能的(对 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);
    }
}

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);
    }
}
任谁 2024-09-11 21:13:31

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

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

这样,你就能够保持用户能够在组合框中滚动,但也能够防止查询发生

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

不奢求什么 2024-09-11 21:13:31

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

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

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

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
仙女 2024-09-11 21:13:31

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

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.

幼儿园老大 2024-09-11 21:13:31

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

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

狠疯拽 2024-09-11 21:13:30

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

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

仅此而已。如果您的项目处于高级状态,则不需要创建新类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文