在组合框 VB.NET 中禁用鼠标滚轮
有谁知道当组合框或列表框等控件具有焦点时禁用鼠标滚轮的方法?就我的目的而言,组合框就是我所需要的答案。
我设置了一个组合框来触发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ComboBox 控件不允许您轻松覆盖 MouseWheel 事件的行为。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上。
请注意,这也会禁用下拉列表中的滚轮。
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.
Beware that this also disables the wheel in the dropdown list.
如果您对控件进行子类化,这是可能的(对 C# 表示歉意)
If you subclass the control it's possible (apologies for the C#)
其中一个选项是向组合框添加一个处理程序,并在该组合框中解决该情况。我不确定你的代码是如何设置的,但我假设如果你知道事件何时发生,你可以设置某种条件来防止查询发生
这样,你就能够保持用户能够在组合框中滚动,但也能够防止查询发生
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
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
结合此线程上的所有答案,如果您不想创建自定义控件,最好的解决方案是处理鼠标滚轮事件。如果下拉列表,下面还允许滚动列表。
假设您的组合框名为组合框1:
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:
我遇到了完全相同的问题,但发现在执行查询后简单地将控件的焦点更改为另一个控件(例如“查询”按钮本身)效果比完美更好。它还允许我仍然滚动控件,直到 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.
只需将其放在鼠标滚轮事件中或放在适用于此的所有控件的单个处理程序中,也许可以将其称为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
我找到了一个混合响应,将此代码放入 MouseWheel 事件中:
仅此而已。如果您的项目处于高级状态,则不需要创建新类。
I've found a mix response, put this code in the MouseWheel event:
That's all. You don't need to create a new class, if you have your project in an advanced state.