无法在 C# Windows 窗体中触发 MouseWheel 事件
首先,鼠标滚轮事件没有列在 Visual Studio 2008 的事件窗格中,这非常烦人。
不过,我在网上找到了正确的格式,并将其写入我的代码中:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
...当鼠标滚轮旋转时,我没有得到任何响应。
我在代码的主类区域中执行此操作,并且设计器仅包含一个表单/窗口/其他内容,因此鼠标不会失去焦点。
namespace BlahBlah
{
public partial class Form1 : Form
{
相比之下,我在鼠标滚轮上方有这个方法,它工作得很好:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
如果我不得不猜测,我想我没有正确地将代码链接到表单(又名:Visual Studio 会做的所有事情对我来说,如果我通过设计器的事件面板添加此事件)。 但我可能是错的,或者只是犯了一些愚蠢的错误。
当鼠标滚轮旋转时,你能帮我得到任何类型的响应吗? 谢谢!
First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.
I found the correct format online though, and wrote this into my code:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
...from which I'm getting no response when the mousewheel is rotated.
I'm doing this in my code's main class area, and the designer contains only the one form/window/whatever so the mouse isn't losing focus.
namespace BlahBlah
{
public partial class Form1 : Form
{
And by contrast, I have this method right above the mousewheel one and it works fine:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
If I had to guess, I'm thinking I'm not correctly linking the code to the form (aka: all the stuff that visual studio would do for me if I added this event through the designer's events panel). But I could be wrong or just be making some silly error.
Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
需要设置鼠标滚轮事件。
将其添加到 Form1 的构造函数中(在 InitalizeComponents(); 之后)
The mousewheel event needs to be set up.
Add this to Form1's constructor (After InitalizeComponents();)
我没有足够的声誉来回复评论,但你的附带问题的答案是代表确实需要设置。 但是,当您通过在属性窗格中双击它来创建代码时,代码会自动生成并放置在 InitializeComponent 方法中的 *.designer.cs 文件中。
I don't have enough reputation to respond with a comment, but the answer to your side question is that the delegates do need to be setup. However when you create one by double clicking it in the properties pane the code is automatically generated and placed in the *.designer.cs file in the InitializeComponent method.
在这种情况下,最好重写
OnMouseWheel
成员函数,而不是注册该事件。 例如:It's best in this case to override the
OnMouseWheel
member function rather than register to the event. For example:我不知道如何订阅该特定事件,但您可以重写表单上的适当方法。
I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.