Click here to Skip to main content
13,050,319 members (129,425 online)
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 13 Mar 2002

Setup a Tab Control Easily to Show and Hide Objects With STabCtrl

, 13 Mar 2002
Rate this:
Please Sign up or sign in to vote.
Set up a tab control easily to show and hide objects with STabCtrl

Sample Image - STabCtrl.gif


I first submitted this code and article way back in Feb '99 and Jul '99 on the CodeGuru site. Wow, do the years go by fast! It's still there and going strong, and I still use the control today in newer projects that I've worked on since the one in whcih the control first turned up in. I've never had a need to change the code so far, but have decided to re-submit here on CodeProject.

Anyway, a lot of projects that I've working make use 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 requires 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 extremely 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 available pages.
  3. Use the SetCurSel member to programmatically show a tab's 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.
    // attach tree control to first page 
    // attach list control to second page 
    // attach combo box control to third page

    // initialize tab to first page. 


Anyway...hope this is of use to everyone!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Australia Australia
Parko, aka Simon Parkinson-Bates, spends his:
1. Days cutting code
2. Evenings building & fixing autonomous planes & quads
3. Nights cutting code, or trying to convince his wife that he just needs a little more time to finish some code off

You may also be interested in...


Comments and Discussions

QuestionIt couldn't work under SDI? Pin
Syouki_kou6-May-08 17:15
memberSyouki_kou6-May-08 17:15 
AnswerRe: It couldn't work under SDI? Pin
Member 319503519-Jan-09 4:31
memberMember 319503519-Jan-09 4:31 
GeneralUsing CSTabCtrl without a resource entry Pin
Michael Farrugia11-Jun-07 11:00
memberMichael Farrugia11-Jun-07 11:00 
QuestionXP static background issue Pin
CASILASI16-Apr-06 0:29
memberCASILASI16-Apr-06 0:29 
GeneralRe: XP static background issue Pin
Douglas Fraser28-Apr-08 3:50
memberDouglas Fraser28-Apr-08 3:50 
QuestionIs it possible to do multiline (stacked ) tabs with STabCtrl? Pin
JimatKC28-Mar-06 16:05
memberJimatKC28-Mar-06 16:05 
AnswerRe: Is it possible to do multiline (stacked ) tabs with STabCtrl? Pin
Parko28-Mar-06 17:26
memberParko28-Mar-06 17:26 
GeneralRe: Is it possible to do multiline (stacked ) tabs with STabCtrl? Pin
JimatKC28-Mar-06 20:22
memberJimatKC28-Mar-06 20:22 
General'AttachControlToTab' : is not a member of 'CTabCtrl' Pin
pepeyman19-Apr-05 2:58
memberpepeyman19-Apr-05 2:58 
GeneralRe: 'AttachControlToTab' : is not a member of 'CTabCtrl' Pin
Parko19-Apr-05 12:11
memberParko19-Apr-05 12:11 
GeneralSTabCtrl without resource Pin
Fred_12323-Jan-03 20:23
memberFred_12323-Jan-03 20:23 
GeneralRe: STabCtrl without resource Pin
matlin12329-Mar-05 11:06
membermatlin12329-Mar-05 11:06 
GeneralRe: STabCtrl without resource Pin
Parko29-Mar-05 14:48
memberParko29-Mar-05 14:48 
QuestionResource View?? Pin
anonymous26-Sep-02 9:46
sussanonymous26-Sep-02 9:46 
AnswerRe: Resource View?? Pin
Parko27-Sep-02 1:52
memberParko27-Sep-02 1:52 
GeneralRe: Resource View?? Pin
stealth kid31-Aug-03 22:49
memberstealth kid31-Aug-03 22:49 
QuestionPaint? Pin
Tobias1-Jul-02 3:12
memberTobias1-Jul-02 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 14 Mar 2002
Article Copyright 2002 by Parko
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid