vivek jain
vivek jain

Reputation: 79

How to check Mouse Out on a Tab area of TabPage in C# Winform?

I want to check Mouse In/Out on Tab Area of TabPage in C# Winform.

There are Event MouseLeave, MouseEnter, MouseMove, but there work for whole TabPage. I just want for Tab only.

TabControl tabControl = new TabControl();
TabPage tabpage = new TabPage();
tabpage.MouseMove += new System.Windows.Forms.MouseEventHandler(panel1_MouseMove);
tabControl.Controls.Add(tabpage);
this.Controls.Add(tabControl);

I'm thinking that If I get to know the Tab area so that I can write Code in MouseMove event for the same, Is there any better way to do the same.

I want for the area pointed by the arrow in the attached image.

Tab

Upvotes: 2

Views: 1294

Answers (2)

LarsTech
LarsTech

Reputation: 81655

The GetTabRect function would help you here:

TabPage mouseTab = null;

void tabControl1_MouseMove(object sender, MouseEventArgs e) {
  TabPage checkTab = null;

  for (int i = 0; i < tabControl1.TabPages.Count; ++i) {
    if (tabControl1.GetTabRect(i).Contains(e.Location)) {
      checkTab = tabControl1.TabPages[i];
      break; // To avoid unnecessary loop
    }
  }

  if (checkTab == null && mouseTab != null) {
    mouseTab = null;
  } else if (checkTab != null) {
    if (mouseTab == null || !checkTab.Equals(mouseTab)) {
      mouseTab = checkTab;
      // or do something here...
    }
  }
}

And to handle the mouse leaving the tab header area:

void tabControl1_MouseLeave(object sender, EventArgs e) {
  if (mouseTab != null) {
    // do something here with mouseTab...

    mouseTab = null;
  }
}

Upvotes: 4

spaceman
spaceman

Reputation: 1101

You can place a Panel control on the whole area of the Tab, and then use the events you mentioned for that Panel control..

Upvotes: 0

Related Questions