How to prevent delay loading of a UserControl in a TabControl?

Deadly 提交于 2019-12-07 13:51:18

问题


I have just discovered that UserControls in a TabControl will not load until the parent TabPage is selected. Is there a way to prevent this delay loading? I need to initialize the UserControls when the main form loads.


回答1:


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.

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.




回答2:


You can invoke Tabcontrol's SelectTab() method for the tabs in your Form's Load event handler.




回答3:


I was just searching how to achieve this default behaviour you describe. An application I support was not delaying the load of the tabs. Turns out the tabs were getting initialised in the load event instead of the constructor.

So if you add the tabs to the tabcontrol in the form load event all the controls in tabs will have their load events fired as a part of the TabPages.AddRange call



来源:https://stackoverflow.com/questions/4131130/how-to-prevent-delay-loading-of-a-usercontrol-in-a-tabcontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!