简体   繁体   中英

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?

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.

Just to be safe...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM