views:

9463

answers:

5

Hi,

I can't get the Mouse Wheel event in the main form.

As a demo I came up with a simple example:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);

        Form2 f2 = new Form2();
        f2.Show(this);
    }

    private void panel1_MouseWheel(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0)
        Console.Out.WriteLine(e.Delta);
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        this.MouseMove += new MouseEventHandler(Form2_MouseMove);
        this.MouseWheel += new MouseEventHandler(Form2_MouseMove);
    }

    private void Form2_MouseMove(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0)
            Console.Out.WriteLine(e.Delta);
    }
}

I get the mouse wheel event in Form2 but not Form1 any ideas?

Cheers,

James

A: 

Maybe this will work for you?

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Form2 f2 = new Form2();
        f2.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        f2.MouseMove += new MouseEventHandler(panel1_MouseWheel);
        f2.Show(this);
    }

    private void panel1_MouseWheel(object sender, MouseEventArgs e)
    {
        if(e.Delta != 0) Console.Out.WriteLine(e.Delta);
    }
}
radekg
A: 

I think you have slightly misunderstood my issue. On my main form with the below code I don'g get the MouseWheel event:

public Form1()
    {
        InitializeComponent();

        this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);    
    }

But I do get the event with:

public Form1()
    {
        InitializeComponent();
        this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);

    }

I hope that is clearer without the Form2 confusion. I am simply trying to get MouseWheel events on a Panel in my main form.

Cheers,

James

+4  A: 

Your problem arises from the fact that form1 has focus, not panel1. ...which ofcourse means that it is form1's events that will be fired, not panel1's events.

I recreated your scenario with the following changes to the constructor in Form1 and verified that it fires the scroll wheel event.

public Form1()
{
        InitializeComponent(); 

        /*  --- Old code that don't work ---
            this.panel1.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
            this.panel1.MouseMove += new MouseEventHandler(panel1_MouseWheel);
        */

        this.MouseWheel += new MouseEventHandler(panel1_MouseWheel);
        this.MouseMove += new MouseEventHandler(panel1_MouseWheel);

        Form2 f2 = new Form2();
        f2.Show(this);
    }
}
Presidenten
A: 

The Panel can't have the focus itself, only an item placed inside the panel can have the focus. The panel will only receive the MouseWheel event once something is placed inside it and that thing has the focus. Simply hoevering over the panel and moving the mouse wheel will send the event to the form, not to the panel.

This is the difference between your two examples.

Sam Meldrum
Not true.In an app I'm wriitng, I have a panel that is my graphical display space. While you can't click-focus the panel, you can say (in the code) <code>myPanel.Focus()</code> so that the panel takes the focus away from everything else in order for my display code to work.
Jerry
@Jerry - I have tested this, and my test shows I am right. If you have nothing on the panel, even calling myPanel.Focus() will not change where the MouseWheel events go. I can only get MouseWheel events when something on the panel has focus.
Sam Meldrum
+1  A: 

Add another event of panel MouseEnter and in its Callback function get the input focus

void MouseEnterEvent() { this.Panel.Focus(); }