Click here to Skip to main content
6,292,811 members and growing! (10,320 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » General     Intermediate License: The Code Project Open License (CPOL)

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

By Parko

Set up a tab control easily to show and hide objects with STabCtrl
VC6Win2K, MFC, Dev
Posted:13 Mar 2002
Views:114,142
Bookmarked:27 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 4.98 Rating: 3.83 out of 5
1 vote, 11.1%
1

2
2 votes, 22.2%
3
1 vote, 11.1%
4
5 votes, 55.6%
5

Sample Image - STabCtrl.gif

Introduction

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() 
{ 
    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.

    // attach tree control to first page 

    m_TabCtrl.AttachControlToTab(&m_TreeCtrl,0); 
    // attach list control to second page 

    m_TabCtrl.AttachControlToTab(&m_ListCtrl,1); 
    // attach combo box control to third page

    m_TabCtrl.AttachControlToTab(&m_ComboCtrl,2); 

    // initialize tab to first page. 

    m_TabCtrl.SetCurSel(0); 

    //////////////////////////////////////////////////////// 

}

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

License

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

About the Author

Parko


Member
Parko, aka Simon Parkinson-Bates, spends his days cutting code, his evenings chasing after his 5.5 year old daughter and cleaning up after his 3.5 year old son, and his nights cutting code.
Occupation: Web Developer
Location: Australia Australia

Other popular Tabs & Property Pages articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
QuestionIt couldn't work under SDI? PinmemberSyouki_kou18:15 6 May '08  
AnswerRe: It couldn't work under SDI? PinmemberMember 31950355:31 19 Jan '09  
GeneralUsing CSTabCtrl without a resource entry PinmemberMichael Farrugia12:00 11 Jun '07  
QuestionXP static background issue PinmemberCASILASI1:29 16 Apr '06  
GeneralRe: XP static background issue PinmemberDouglas Fraser4:50 28 Apr '08  
GeneralIs it possible to do multiline (stacked ) tabs with STabCtrl? PinmemberJimatKC17:05 28 Mar '06  
GeneralRe: Is it possible to do multiline (stacked ) tabs with STabCtrl? PinmemberParko18:26 28 Mar '06  
GeneralRe: Is it possible to do multiline (stacked ) tabs with STabCtrl? PinmemberJimatKC21:22 28 Mar '06  
General'AttachControlToTab' : is not a member of 'CTabCtrl' Pinmemberpepeyman3:58 19 Apr '05  
GeneralRe: 'AttachControlToTab' : is not a member of 'CTabCtrl' PinmemberParko13:11 19 Apr '05  
GeneralSTabCtrl without resource PinmemberFred_12321:23 23 Jan '03  
GeneralRe: STabCtrl without resource Pinmembermatlin12312:06 29 Mar '05  
GeneralRe: STabCtrl without resource PinmemberParko15:48 29 Mar '05  
GeneralResource View?? Pinsussanonymous10:46 26 Sep '02  
GeneralRe: Resource View?? PinmemberParko2:52 27 Sep '02  
GeneralRe: Resource View?? Pinmemberstealth kid23:49 31 Aug '03  
GeneralPaint? PinmemberTobias4:12 1 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Mar 2002
Editor: James Spibey
Copyright 2002 by Parko
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project