Tom
Tom

Reputation: 317

C# getting mouse coordinates from a tab page when selected

I wish to show the mouse coordinates only when I am viewing tabpage7.

So far I have:

this.tabPage7.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove);

protected void OnMouseMove(object sender, MouseEventArgs mouseEv)
      {
          Console.WriteLine("happening");
          Console.WriteLine(mouseEv.X.ToString());
          Console.WriteLine(mouseEv.Y.ToString());
      }

but this doesn't appear to be doing anything, could someone help show me what I'm doing wrong please?

Upvotes: 0

Views: 1188

Answers (2)

Hans Passant
Hans Passant

Reputation: 942000

Hard to tell what you did wrong, your code is not complete. This works:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        tabPage2.MouseMove += new MouseEventHandler(tabPage2_MouseMove);
    }
    private void tabPage2_MouseMove(object sender, MouseEventArgs e) {
        Console.WriteLine(e.Location.ToString());
    }
}

Note that if the tab page contains any controls then those controls will get the mouse move message, not the tab page. Also note that overloading the form's OnMouseMove() method is not a good idea, even though you'll get away with it in this specific case.

Upvotes: 1

MQS
MQS

Reputation: 413

Just to be safe...

Where are you subscribing to the MouseMove event? ( where is this.tabPage7.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMove); )

Upvotes: 0

Related Questions