Set up a tab control easily to show and hide objects with STabCtrl

Sample Image

Environment: Visual C++ 4.2, Windows NT 4 (SP3)

Since I first sumitted this article (way back in Feb 99) and "STabCtrl" people out there have been responding with thanks, bug fixes and enhancements, thanks all. It's been a while now since the first submission so I thought that I'd better get a more up to date version of STabCtrl submitted.

On the outside the control hasn't changed much, Only one bug fix and the additional handling of the OnEnable message. Internally a fair bit has changed.. a small amount of optimization and some major house cleaning of the code.

Anyway, the current project that I'm working makes use of a lot of tab controls, and on the tab pages there are typically up to
10 controls! And using the ON_NOTIFY(TCN_SELCHANGE... ) message, in the parent window becomes painful.
Why not use property pages? people ask. Sure that would make things easier but the pages on property pages take up the entire parent window and we've got layouts that require any number of controls to not be on a page but around the tab control somewhere, to do that with property pages requries a bit of painful coding to get things where you want them, and if scaling of the page is involved it's even harder!

The STabCtrl although very simple, I've found to be extremly useful. It Implements a tab control that automatically handles the showing and hiding of objects attached to a tab's various pages eliminating the need to do so via the ON_NOTIFY(TCN_SELCHANGE... ) message in a parent window.

To use the control:

  1. Simply replace any instance of a CTabCtrl with CSTabCtrl (remember to include the header file "STabCtrl.h"),
    initialize it as you would an MFC CTabCtrl.
  2. Use the AttachControlToTab member to attach you objects
    to the various avaliable pages.
  3. Use the SetCurSel member to programaticly show a tabs particular page.

Note: Steps 1, 2 & 3 are typically done in the parent dialogs ::OnInitDialog() member. Once done the tab control will show and hide the attached objects depending on the users tab selection.

// file : SomeDialogClass.h 

class CSomeDialogClass : public CDialog 
	CSTabCtrl m_TabCtrl; 
	CTreeCtrl m_TreeCtrl; 
	CListCtrl m_ListCtrl; 
	CComboBox m_ComboCtrl; 

	virtual BOOL OnInitDialog(); 

// file : SomeDialogClass.cpp

BOOL CSomeDialogClass::OnInitDialog() 

	// set up tabs. 

	PSTR pszTabItems[] = 
		"Tab Sheet 1 : Tree control", 
		"Tab Sheet 2 : List control", 
		"Tab Sheet 3 : Combobox control", 

	TC_ITEM tcItem; 

	for(INT i = 0; pszTabItems[i] != NULL; i++) 
		tcItem.mask = TCIF_TEXT; 
		tcItem.pszText = pszTabItems[i]; 
		tcItem.cchTextMax = strlen(pszTabItems[i]); 

	// attach controls to tabs pages.

	m_TabCtrl.AttachControlToTab(&m_TreeCtrl,0); // attach tree control to first page 
	m_TabCtrl.AttachControlToTab(&m_ListCtrl,1); // attach list control to second page 
	m_TabCtrl.AttachControlToTab(&m_ComboCtrl,2); // attach combo box control to third page

	// initialize tab to first page. 



Download demo project - 18 Kb
Download source - 3 Kb