需要用鼠标滚轮滚动用户控件
我有一个我创建的用户控件。 我在其右侧添加了一个面板和一个垂直滚动条。 我希望能够用鼠标滚轮滚动它。 问题是似乎没有任何事件在鼠标滚轮上触发。 如果我取下面板,则用户控件具有焦点,它将在表单中的鼠标滚轮上触发。 但是,打开面板后,它似乎不会触发面板的鼠标滚轮事件,也不会触发控件内甚至表单上的用户控件。 最好的解决方案是在用户控件中触发事件,但我什至会接受表单上的事件并将其反馈到用户控件中。
如果重要的话,我正在使用 vb.net 和 vs2005。
I have a usercontrol I created. I added a panel and a vertical scrollbar to the right of it. I'd like to be able to scroll it with the mousewheel. The problem is there doesn't seem to be any events that fire on mousewheel. If I take the panel off then the usercontrol has focus and it will fire on mousewheel in the form. But with the panel on it doesn't seem to fire the mousewheel event of the panel, or the usercontrol within the control or even on the form. The best solution would be to have an event fire in the usercontrol but I'd even accept an event on the form and feed it back into the usercontrol.
I'm using vb.net and vs2005 if it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,面板可以有焦点。 你只需要给它焦点,我更喜欢在鼠标悬停时使用。
我这样做了,问题就解决了。
yes, a panel can have focus. You just have to give it focus, I prefer to use on mouse over.
I did this and its problem solved.
我一整天都在研究这个问题,我可能已经弄清楚了。 鼠标滚轮事件仅发送到具有焦点的控件。 面板无法获得焦点。 由于面板覆盖了用户控件,因此它也无法获得焦点。 (除非它是表单上的唯一控件)如果在面板上 mouseenter 事件我调用 me.focus 它将焦点设置到用户控件,允许它接收鼠标滚轮事件。 该事件在窗体和控件中触发。 如果有更好的方法,我仍然愿意接受建议,因为这看起来有点老套。
I've been researching this all day, I may have figured this out. The mousewheel event is only sent to the control with focus. A panel can't have focus. Since the panel is covering the usercontrol it can't get the focus either. (unless it's the only control on the form) If on the panel mouseenter event I call me.focus it sets the focus to the usercontrol allowing it to receive the mousewheel event. The event fires in the form and the control both. I'm still open to suggestions if there's a better way though, as this seems a little hacky.
将以下代码放置在窗体的load事件中
“运行项目”表单的加载事件中。
您应该在 Panel 控件内看到一个包含 100 个按钮的窗体。
Panel 控件应包含一个垂直 ScrollBar。
使用面板内的滚轮应滚动按钮。
希望这个例子有帮助。
编辑
这不是正确的做法。
您需要使用面板的
AutoScroll
属性。编辑
另一个示例:
将以下代码粘贴到表单加载事件中
运行程序。
单击按钮(在表单上)。
请注意,您必须单击 UserControl 才能设置其焦点并使用滚轮。
Place the following code in the load event of the form
Run the project.
You should see a form with 100 buttons inside the Panel control.
The Panel control should contain a vertical ScrollBar.
Using the scroll wheel inside the Panel should scroll through the Buttons.
Hope this example helps.
Edit
That is not the correct way to do it.
You need to use the
AutoScroll
property of the Panel.Edit
Another Example:
Paste the following code in the form load event
Run the program.
Click the Buttons (on the Form).
Notice, that you have to click the UserControl to set its focus and use the scroll wheel.
这里是一篇关于在 vb.net 中使用鼠标事件的文章。 它特别提到了鼠标滚动。
Here is an article about working with mouse events in vb.net. It specifically mentions mouse scroll.
您还可以添加以下代码行:
这允许您在上下移动滚动条时看到控件中的项目。 如果没有它,控件仅在释放滚动条时滚动。
You might also add the following line of code:
This allows you to see items in the control as you move the scrollbar up and down. Without it the control only scrolls when the scrollbar is released.