且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止TabControl中的UserControl延迟加载?

更新时间:2023-12-06 17:33:04

TabControl不会特别对待其控件,实际上,在所有情况下,UserControl上的Load事件立即发生都是正常的第一次显示该控件之前. TabPage负责显示控件,因此仅在首次选择时才会加载".

The TabControl does not treat its controls specially, in fact it is normal under all circumstances for the Load event on a UserControl to occur immediately before the control is displayed for the first time. The TabPage is responsible for showing the control, therefore it will only be 'loaded' when first selected.

要克服这种(完全正常的)Windows窗体行为,您可以将初始化代码移至单独的方法,并在Form加载时调用它,或者可以将初始化代码放入UserControl的构造函数中反而.无论哪种方式,您都可以立即执行初始化.

To overcome this (perfectly normal) Windows Forms behaviour, you could move your initialisation code to a separate method and call it when the Form loads, or you could just place your initialisation code in the UserControl's constructor instead. Either way, you can perform your initialisation immediately.