John_Sheares
John_Sheares

Reputation: 1414

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

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.

Upvotes: 4

Views: 2818

Answers (3)

Daniel
Daniel

Reputation: 11

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

Upvotes: 1

Adam Butler
Adam Butler

Reputation: 3037

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

Upvotes: 0

Bradley Smith
Bradley Smith

Reputation: 13621

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.

Upvotes: 2

Related Questions