WinForms选项卡页事件未触发 [英] winforms tab page event not firing
问题描述
我有一个TabControl
,用户可以在其中添加标签页.
I have a TabControl
to which the user can add tab pages.
我正在尝试附加一些事件,例如:MouseEnter
,MouseLeave
,MouseClick
,但是这些事件似乎根本没有触发,只有在将它们附加到
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);
}
推荐答案
您不需要为此事件,因为默认情况下,最终用户无法在没有提供代码的情况下将TabPages添加到TabControl.
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.
因此,无论您在何处添加TabPage,都应在这些事件之间进行接线:
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);
这篇关于WinForms选项卡页事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!