Can't fire MouseWheel event in C# Windows Forms

ck_ picture ck_ · Jul 27, 2009 · Viewed 16.9k times · Source

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!

Answer

Kurisu picture Kurisu · Jul 27, 2009

The mousewheel event needs to be set up.

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

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);