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


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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



  • Tree control getting grayed if I select an item on tab change

    Posted by Legacy on 09/11/2003 07:00am

    Originally posted by: Binayak


    First off, I'll thank you for this wonderful code. I used it in my application to show two TreeControls which are toggled by the tab control. Now, the problem is that, when tab is changed I want to select an item from the changed tree (maybe the root item of the tree). As sooon as I select any item from the tree on tab change the whole tree becomes gray. If I click on the tree control I am able to see the items, otherwise not.

    Here is the code that I'm using (very simple).

    Please Help.

    if (m_TabCtrl.GetCurSel( ) ==1 )
    m_TreeCtrl2.SelectItem (m_TreeCtrl2.GetRootItem());


    if (m_TabCtrl.GetCurSel( ) ==0 )

    m_TreeCtrl1.SelectItem (m_TreeCtrl1.GetRootItem());


  • How to include a tab control in a web page?

    Posted by Legacy on 08/10/2003 07:00am

    Originally posted by: Sachin

    I am trying to get some stuff to use a tab control like feature in my web page that on clicking on each different tab bar open a new window link. Can anyone help me regarding this. Can it done using HTML,vbscript,javascript or any other language.please be in touch with me if anyone has a solution.

  • Very Useful...but how can I create scrollbar in the dialog embedded in the tab?

    Posted by Legacy on 07/15/2003 07:00am

    Originally posted by: Yuki

    I am using eVC++ 3.0 and MFC to create a dialog-based application. I don't know how to make the dialogs insides the tab scrollable. Do I have to use Property Sheet to do so? Or I can just use my CDialog to do so?

  • Tab Control Without using MFC

    Posted by Legacy on 06/24/2003 07:00am

    Originally posted by: Chakresh Maheshwari

    Dear Friends,
    The code was nice but what I want is bit different.
    I want same result but without using MFC.I am using only WIN32 API directly without MFCs.
    Please Help me

  • Nice code. I do something similar now.

    Posted by Legacy on 10/31/2000 08:00am

    Originally posted by: Serguei Batchila

    I found something new for myself - ON_NOTIFY_REFLECT_EX

    And I can give you advice - instead of keeping prev selected tab in m_nPrevSel var, you can handle TCN_SELCHANGING - in the handler of TCN_SELCHANGING function GetCurSel() will return you a prev selected tab.
    Your code will look like:
    BOOL CSTabCtrl::OnSelchange(NMHDR*, LRESULT*)
    INT _nCurrSel = GetCurSel();
    m_TabPagesMap.ShowWindows (_nCurrSel, SW_SHOW);
    *pResult = 0;
    return FALSE; // allow control to handle as well.

    BOOL CSTabCtrl::OnSelchanging(NMHDR*, LRESULT*)
    INT _nCurrSel = GetCurSel();
    m_TabPagesMap.ShowWindows(_nCurrSel, SW_HIDE);
    *pResult = 0;
    return FALSE; // allow control to handle as well.

  • nice code, little bug

    Posted by Legacy on 01/25/2000 08:00am

    Originally posted by: Danny24

    hy simon,

    when trying to use your CSTabCtrl in a view with dynamically creation/destruction of the CSTabCtrl i found a little bug in your code. In your overriden OnCreate you should call the CTabCtrl::OnCreate, otherwise every try to add tabs via InsertItem or SetItem will return with non-failure codes, but if you try to attach controls you will get the assertion that the page you try to add to is not existent...


  • A Question on Tab Controls

    Posted by Legacy on 12/27/1999 08:00am

    Originally posted by: Pushpak Karnick

    i dont knoe if this is the right place to put this question, but here goes :->

    i want to have a MSFlexgrid (or any grid, for that matter) on a tab control. The standard controls supplied by the developer studio do appear on the tab control when i test the dialog (using Ctrl-T) .. but the grid control does not .. do suggest a way to do this ..

    thanks in advance


  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date