Click here to Skip to main content
15,918,516 members
Articles / Desktop Programming / MFC
Article

CXTabCtrl: an easier tab control for dialogs and forms

Rate me:
Please Sign up or sign in to vote.
4.50/5 (32 votes)
26 Jun 2000 379.6K   7.8K   77   79
An easier tab control

Sample Image - CXTabControl.gif

Introduction

When I first started using Microsoft VC++, I found it boring to put a tab control in a form or in a dialog. I had to create the dialogs, fill in the TCITEM structure for each of my tabs, and finally insert them into the tab control. With CXTabCtrl it is far simpler, as is illustrated below:-

InitDialog (CDialog) or InitialUpdate (CFormView) 

...
m_pMyDlg = new CMyDlg;
m_pMyDlg->Create(CMyDlg::IDD, &m_tabctrl /*CXTabCtrl variable*/);
m_tabctrl.AddTab(m_pMyDlg, "Tab caption", 0 /* image number*/);
...

The OnSelChange that you previously had to implement in the dialog (or form) to show the correct dialog is now in the CXTabCtrl, so you don't have to worry about it. You can even disable a tab so the user cannot see its contents.

m_tabctrl.EnableTab(1 /*index*/, FALSE);

You can also change the color for the following states of each tab:-

  • Selected tab
  • Unselected tab
  • Disabled tab
  • A mouse over tab
  • You can select a tab from it's caption, or dynamically change a tab. In addition to this, you can select the previous or next tab with the SelectNextTab method.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Architect VisionOne AG
    Switzerland Switzerland
    XicoLoko is a brazilian developer based in Switzerland.

    Comments and Discussions

     
    AnswerRe: Can I get the sample project with FORMVIEW (not dialog based)? Pin
    23-Jun-01 0:53
    suss23-Jun-01 0:53 
    GeneralRe: Can I get the sample project with FORMVIEW (not dialog based)? Pin
    24-Jul-01 12:01
    suss24-Jul-01 12:01 
    GeneralRe: Can I get the sample project with FORMVIEW (not dialog based)? Pin
    24-Aug-01 3:15
    suss24-Aug-01 3:15 
    GeneralRe: Can I get the sample project with FORMVIEW (not dialog based)? Pin
    7-Nov-01 15:02
    suss7-Nov-01 15:02 
    GeneralSpawn a new window Pin
    27-Apr-01 11:38
    suss27-Apr-01 11:38 
    GeneralRe: Spawn a new window Pin
    20-Aug-01 2:30
    suss20-Aug-01 2:30 
    GeneralUse class with existing Property Sheet Pin
    Bob Eastman14-Apr-01 16:51
    Bob Eastman14-Apr-01 16:51 
    GeneralThree Improvements Pin
    Darren S. Gonzales15-Feb-01 10:30
    Darren S. Gonzales15-Feb-01 10:30 
    This is an excellent class. It gave me a great starting point. I just wanted to mention three fixes that I made.

    First, the class doesn't allow the parent to validate the tab when the user clicks on a new tab. I changed the ON_NOTIFY_REFLECT to ON_NOTIFY_REFLECT_EX, and return FALSE from OnSelchanging() (note that the MSDN says to return TRUE to allow the parent to handle the notification, but the MSDN is wrong!)

    Second, the function SelectTab() handles all changing of tab by itself, and once again does not give the current tab a chance to validate itself. Instead of just changing the tab right
    there, I modified this function to send the appropriate notification messages. The following shows my change:

    BOOL CXTabCtrl::SelectTab(int iIndex)
    {
    .
    .
      if (m_arrayStatusTab[iIndex])
      {
        NMHDR nmhdr;
          // make sure we can select the tab
        nmhdr.hwndFrom = m_hWnd;
        nmhdr.idFrom = GetDlgCtrlID();
        nmhdr.code = TCN_SELCHANGING;
        if (GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), &nmhdr))
        {
          return FALSE;
        }
    
          // change the page
        SetCurSel(iIndex);
        nmhdr.code = TCN_SELCHANGE;
        GetParent()->SendMessage(WM_NOTIFY, GetDlgCtrlID(), &nmhdr);
        return TRUE;
      }
    .
    .
    }

    This makes the tab change in the exact same way as if the user had clicked on the tab. The control will handle these notifications and the tab will change, as long as it validates.

    The third change that I made is with the focus. I noticed that if the focus is set to an item on a page, and you switch tabs, the invisible item still has the focus. For example, in the sample app, highlight the "ID" edit box and type a number, then switch tabs and type another number, then return back to the first tab. The ID edit box still has focus and it shows both numbers that you typed (even though the window wasn't visible when you typed the second number).

    To fix this, I added the following to the OnSelChange() handler:

    void CXTabCtrl::OnSelchange(NMHDR* pNMHDR, LRESULT* pResult)
    {
    .
    .
        pWnd->ShowWindow(SW_HIDE);
        CWnd *pFocusWnd = GetFocus();
        while (pFocusWnd)
        {
          if (pFocusWnd == pWnd)
          {
            ::SetFocus(NULL);
            pFocusWnd = NULL;
          } else {
            pFocusWnd = pFocusWnd->GetParent();
          }
        }
    .
    .
    }


    What this does is, if the focus is set to any window inside the CWnd that gets hidden, then it gets reset to NULL.

    Hope these changes help somebody. Smile | :)
    GeneralRe: Three Improvements Pin
    3-Jul-01 13:42
    suss3-Jul-01 13:42 
    GeneralRe: Three Improvements Pin
    lpvoid6-Nov-02 3:18
    lpvoid6-Nov-02 3:18 
    GeneralRe: Three Improvements Pin
    lpvoid6-Nov-02 3:20
    lpvoid6-Nov-02 3:20 
    GeneralThe tabclass control cannot be used on already created dialogs by MFC wizards. Pin
    31-Jan-01 1:43
    suss31-Jan-01 1:43 
    GeneralRe: The tabclass control cannot be used on already created dialogs by MFC wizards. Pin
    10-Oct-01 5:20
    suss10-Oct-01 5:20 
    GeneralThe tabclass control cannot be used on already created dialogs by MFC wizards. Pin
    31-Jan-01 1:42
    suss31-Jan-01 1:42 
    GeneralOnsize when showing dialog Pin
    Matt Philmon4-Oct-00 7:50
    Matt Philmon4-Oct-00 7:50 
    GeneralRe: Onsize when showing dialog Pin
    Matt Philmon4-Oct-00 10:58
    Matt Philmon4-Oct-00 10:58 
    GeneralRe: Onsize when showing dialog Pin
    25-Apr-02 14:06
    suss25-Apr-02 14:06 
    QuestionTab not visible? Pin
    Goh Choon Liang26-Sep-00 6:33
    Goh Choon Liang26-Sep-00 6:33 
    AnswerRe: Tab not visible? Pin
    Matt Philmon2-Oct-00 16:24
    Matt Philmon2-Oct-00 16:24 
    GeneralRe: Tab not visible? Pin
    31-Jan-01 23:00
    suss31-Jan-01 23:00 
    AnswerRe: Tab not visible? Pin
    TJ Houston6-Jun-02 22:56
    TJ Houston6-Jun-02 22:56 
    AnswerRe: Tab not visible? Pin
    nomadik12-Jan-03 22:30
    nomadik12-Jan-03 22:30 

    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.