问题
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