Question

I have a TabControl to which the user can add tab pages.

I am trying to attach some events to it such as: MouseEnter, MouseLeave, MouseClick, But it seems the these events are not firing at all, they only fire when I attach them to the TabControl itself, but this is not what I need.

What is the problem with attaching events to a tab control tab page ?

Here is my latest attempt to attach these event from my code:

private void customerTabCtrl_ControlAdded(object sender, ControlEventArgs e)
{
   TCTabPage tctab = (TCTabPage)e.Control; // Option A
   TCTabPage tctab = (TCTabPage)customerTabCtrl.Controls[customerTabCtrl.Controls.Count - 1]; //Option B
   tctab.MouseEnter += new EventHandler(tctab_MouseEnter);
   tctab.MouseLeave += new EventHandler(tctab_MouseLeave);
}
Was it helpful?

Solution 2

You don't need an event for this since by default, the end user cannot add TabPages to the TabControl without you providing the code for it.

So wherever you are adding the TabPage, that's when you should wiring up those events:

TCTabPage tctab = new TCTabPage();
tbtab.Text = "New Tab";
tctab.MouseEnter += tctab_MouseEnter;
tctab.MouseLeave += tctab_MouseLeave;
customerTabCtrl.TabPages.Add(tctab);

OTHER TIPS

I fill so silly... I found out the "problem", I thought that the MouseEnter, MouseLeave, MouseClick events should fire even when the cursor is on the tab header, but it appears that these events fire only when the cursor is at the tab body... Sory for the trouble, I am using winforms only 6 months now...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top