views:

1680

answers:

4

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!

+1  A: 

Where are you wiring this event up? Paste that code in as well. Also, what file is it in, make sure its not in the auto-generated file.

Allen
Thanks, edited description.
cksubs
"Thanks" but no upvote? ;-P
Allen
A: 

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

Mark Simpson
+4  A: 

The mousewheel event needs to be set up.

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

this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
Kurisu
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?
cksubs
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.
Kurisu
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.
cksubs
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.
Kurisu
+5  A: 

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.

marco0009
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.
Allen