无法在C#Windows窗体中触发MouseWheel事件

时间:2021-02-03 20:35:39

First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.

首先,鼠标滚轮事件未在Visual Studio 2008的事件窗格中列出,这非常烦人。

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.

如果我不得不猜测,我认为我没有正确地将代码链接到表单(例如:如果我通过设计器的事件面板添加此事件,Visual Studio将为我做的所有事情)。但我可能是错的,或者只是犯了一些愚蠢的错误。

Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!

当鼠标滚轮旋转时,你能帮助我做出任何反应吗?谢谢!

4 个解决方案

#1


The mousewheel event needs to be set up.

需要设置鼠标滚轮事件。

Add this to Form1's constructor (After InitalizeComponents();)

将其添加到Form1的构造函数(After InitalizeComponents();)

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);

#2


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.

我没有足够的声誉回复评论,但是你的问题的答案是代表确实需要设置。但是,当您通过在属性窗格中双击它来创建一个代码时,代码将自动生成并放在InitializeComponent方法的* .designer.cs文件中。

#3


I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.

我不知道如何订阅该特定事件,但您可以覆盖表单上的相应方法。

#4


It's best in this case to override the OnMouseWheel member function rather than register to the event. For example:

在这种情况下,最好覆盖OnMouseWheel成员函数,而不是注册到事件。例如:

public class MyFrm : Form
{
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        /*Handle the mouse wheel here*/
        base.OnMouseWheel(e);
    }
}

#1


The mousewheel event needs to be set up.

需要设置鼠标滚轮事件。

Add this to Form1's constructor (After InitalizeComponents();)

将其添加到Form1的构造函数(After InitalizeComponents();)

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);

#2


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.

我没有足够的声誉回复评论,但是你的问题的答案是代表确实需要设置。但是,当您通过在属性窗格中双击它来创建一个代码时,代码将自动生成并放在InitializeComponent方法的* .designer.cs文件中。

#3


I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.

我不知道如何订阅该特定事件,但您可以覆盖表单上的相应方法。

#4


It's best in this case to override the OnMouseWheel member function rather than register to the event. For example:

在这种情况下,最好覆盖OnMouseWheel成员函数,而不是注册到事件。例如:

public class MyFrm : Form
{
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        /*Handle the mouse wheel here*/
        base.OnMouseWheel(e);
    }
}