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

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

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 28, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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()
{
	CDialog::OnInitDialog();

	////////////////////////////////////////////////////////
	// set up tabs.

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

	TC_ITEM tcItem;

	for(INT i = 0; pszTabItems[i] != NULL; i++)
	{
		tcItem.mask = TCIF_TEXT;
		tcItem.pszText = pszTabItems[i];
		tcItem.cchTextMax = strlen(pszTabItems[i]);
		m_TabCtrl.InsertItem(i,&tcItem);
	}

	// 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.
	m_TabCtrl.SetCurSel(0);

	////////////////////////////////////////////////////////
}


Downloads

Download demo project – 18 Kb
Download source – 3 Kb

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.