Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 695
John_Sheares Avatar asked Sep 04 '25 04:09

John_Sheares


1 Answers

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.

like image 128
Bradley Smith Avatar answered Sep 07 '25 15:09

Bradley Smith