Windows Develop Bookmark and Share   
 index > Windows Forms General > TabControl: Selected event ignored in Form Load event
 

TabControl: Selected event ignored in Form Load event

I've written initialization code for each of my TabPages in the the Selected event handler. However, when the form where the TabControl lives first loads,

(1) the Selected event of the TabPage that's displayed doesn't fire automatically and (2) programmatically selecting a TabPage in the Form Load event (by using SelectTab or SelectedIndex etc) is ignored and does not fire the Selected event.

How do I cause the Selected event to fire on Form Load, or how do I cause my initialization code to run for the top, visible TabPage? Thanks!

-Doug

Douglas Laudenschlager  Saturday, April 29, 2006 4:28 PM
Doug,

Yes, I've seen this. Very annoying. The underlying problem is that the sequencing of events is not well documented, so you don't know which events fire in which order and thus can assign a value that the system will subsequent override. Also, if you try to select a TabPage that is already selected, I don't believe the Selected event will even fire.

Are you initializing the TabControl itself or the actual TabPages within the control? If the pages, you might try the Enter event instead on each page. If you guarantee the first page receives focus, then the problem is solved. Another option is the Layout event. Layout occurs before Load, while Focus is set (and Enter invoked) after Load.

If in .NET 2.0, you could also try the Form.Shown event, which has the added advantage of only firing once. The Shown event also fires after Load fires and focus is assigned, so any changes you make are likely to stick.

Hope that helps.

Erik
eebrown  Monday, May 01, 2006 1:59 AM

Erik,

Thank you for the information and the suggestions. My goal is to initialize individual TabPages (populate controls, etc) when the user (or program) selects that page. The Selected event works great except on Form Load, where it may not be firing because the TabPage on which I want the event to fire is also the one that's displayed by default.

For the moment I've been using the inelegant workaround of selecting a different TabPage, then immediately reselecting the first TabPage (in Form Load) to force the first page's event to fire. However I'll explore the Enter event and others that you suggest.

Thanks again,

-Doug

Douglas Laudenschlager  Monday, May 01, 2006 1:49 PM

Hi,

Is your problem solved?

Thank you,
Bhanu.

Bhanu Prakash Nunna - MSFT  Thursday, May 11, 2006 9:31 PM

I have not yet experimented with the other events that were suggested to me. I am still "forcing" the Selected event to fire by switching tabs programmatically. Thanks,

-Doug (douglasl)

Douglas Laudenschlager  Thursday, May 18, 2006 10:49 PM

You can use google to search for other answers

Custom Search

More Threads

• Simple Event Help
• SplitContainer Flicker while Resize
• Add TextBox control to Form using CodeDom
• AutoScroll doesn't Paint until MouseUp?
• Printing to a Card Printer (SP55) using C#
• draw order vs. docking order in layout manager
• Trying to get GUI to appear for logged in user, from a service running as System.....
• Combobox dynamic text update issue on SelectedIndexChanged event handler
• Set DB Connection object global for all forms
• Form Closing event NOT firing