Can't fire MouseWheel event in C# Windows Forms
Asked Answered
O

4

7

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!

Oliphant answered 27/7, 2009 at 19:32 Comment(0)
A
14

The mousewheel event needs to be set up.

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

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
Ariadne answered 27/7, 2009 at 19:40 Comment(4)
That's it, thanks much! Sidequestion: I have one for my MouseMove method as well (this.MouseMove += Form1_MouseMove;), but not for MouseClick. Why doesn't it need one?Oliphant
Well... does it respond to a mouse click? If it doesn't need to react to mouse clicks, it will not need one. Some controls automatically have linked up mouse click events (For scrolling and whatnot.) It depends on what you're trying to accomplish.Ariadne
I just mean my MouseClick method works (fires on mouse-click) without the need for the ".... += ...." stuff in the constructor. That's why I forgot about adding something like that for MouseWheel, MouseClick worked just fine.Oliphant
It's possible that the Designer added it for you. What you can do is expand your form and view the code of the Form1.Designer.cs file. In there you will probably find it somewhere.Ariadne
T
6

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.

Trapp answered 27/7, 2009 at 19:55 Comment(1)
Yep, take a look in the designer.cs file and look in the regions, you'll see how the code gen has done a bit of work wiring up these events for you.Rubbery
S
2

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

public class MyFrm : Form
{
    protected override void OnMouseWheel(MouseEventArgs e)
    {
        /*Handle the mouse wheel here*/
        base.OnMouseWheel(e);
    }
}
Scevo answered 13/4, 2012 at 19:29 Comment(1)
@JonofAllTrades It has better performance and more flexibility (you can do your work before any of the delegates that are subscribed to the event). #159817Scevo
P
1

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

Platas answered 27/7, 2009 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.