CXTabCtrl: an easier tab control for dialogs and forms

Sample Image

When I first got in touch with the Microsoft VC, it was boring to put a tab control in a form or in a dialog. I had to create the dialogs, fill in the TCITEM struct for each of my tabs, and finally insert them in the tab control. With the CXTabCtrl what you have to do is ilustrated 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 had to implement in the dialog (or form), to show the correct dialog, now is in the CXTabCtrl, so you don't have to worry about it. You can disable a tab too, so the user cannot see it's contents.
m_tabctrl.EnableTab(1 /*index*/, FALSE);

Also, you can set the following colors:

  • Selected tab
  • Unselected tab
  • Disabled tab
  • A mouse over tab

Downloads

Download demo project - 19Kb

Download source - 4Kb



Comments

  • Great Job

    Posted by Legacy on 09/02/2002 12:00am

    Originally posted by: S.P.Rewatkar

    Thnx,
    This code help me a lot.

    Reply
  • How to use keyboard navigation in attached PropertyPages?

    Posted by Legacy on 06/25/2002 12:00am

    Originally posted by: Ray A. Akey

    How can I make it so that the TAB key changes the focus from the current control in an attached property page to the "next" control in that same property page?

    Currently, hitting TAB only make the focus change from the control that currently has focus, to the TAB control.

    How do I make the focus go to a different control? e.g. TAB cycle focus through ALL the controls on the dialog and then, finally, to the TAB control?

    Reply
  • Simplified Tab Creation Process

    Posted by Legacy on 06/20/2002 12:00am

    Originally posted by: John Alexiou

    This class needs the following code in OnInitDialog()
    
    ...
    CXTabCtrl tabCtrl;
    ...
    mytab = new CMyTab;
    mytab->Create( CMyTab::IDD, &tabCtrl);
    tabCtrl.AddTab( mytab, "A new tab",0);
    mytab->tabCtrlPtr = &tabCtrl;

    I think there is a lot of redundancy in this and if you have a lot of tabs
    keeping up with the pointers to CMyTab classes is a bit of a hasle.
    What I want is to have simple statements like this:

    tabCtrl.AddTab<CMyTabNew>("A new tab",0);
    tabCtrl.AddTab<CMyTabOther>("Another tab",0);
    tabCtrl.AddTab<CMyTabLast>("The last tab",0);

    where CMyTab... are the classes for the forms that are going to be
    displayed in the Tab Control.


    My solution adds the following member to CXTabCtrl
    ...
    vectorPtr<CDialog> forms;
    ...

    and the following method to CXTabCtrl

    template<typename F>
    void AddTab(LPTSTR lpszCaption, int iImage =0)
    {
    const int item = forms.AddLink(new F() );
    ((F *)forms[item])->Create( F::IDD, this);
    AddTab( forms[item], lpszCaption, iImage);
    ((F *)forms[item])->tabCtrlPtr = this;
    }


    all that is left is a class for a vector of pointers with automatic delete such as:

    struct DeletePtr {
    template<typename T>
    void operator()(const T * ptr) const { delete ptr; }
    };

    template<typename R>
    class vectorPtr : public std::vector<R *> {
    public:
    virtual ~vectorPtr()
    {
    std::for_each( begin(), end(), DeletePtr() );
    }
    int AddLink(R * rval)
    {
    std::vector<R *>::push_back(rval);
    return (int)base::size()-1;
    }
    };

    As a result the Main Form Destructor does not need to explicitly delete
    the pointers to CMyTab...classes as in the original CXTabCtrl

    the only requerement on CMyTab (a form class for the tab) is that it contains
    a member for access to the Tab Control

    class CMyTab : public CDialog {
    ...
    CXTabCtrl * tabCtrlPtr;
    ...
    }

    Reply
  • no tabs

    Posted by Legacy on 11/10/2001 12:00am

    Originally posted by: AMW

    I'm pretty sure I've entered everything in right and my program builds and runs, but I have no tabs on my tab control.
    
    I'm writing an SDI app with a formview in a splitter window,
    and I can see the tab control in my formview and everthing, but I have no tabs.
    I'm still kinda new to MFC and any kind of help would really be appreciated.
    Thanks

    Reply
  • I wana get a sample project with FORMVIEW.

    Posted by Legacy on 05/27/2001 12:00am

    Originally posted by: baldr

    I've a problem in selecting tabs in menu command.

    Reply
  • how to remove flicker ?

    Posted by Legacy on 04/20/2001 12:00am

    Originally posted by: troggan

    hi!
    i want to use this in a resizable-dialog. but this is flickering terrible. how can i work around that?

    thanks
    troggan

    Reply
  • Hitting "escape" does not cause dialog to shut down...

    Posted by Legacy on 05/12/2000 12:00am

    Originally posted by: Paul

    I'm using the tab class, but when the user hits "escape" the dialog does not shut down.....is there anything I need to do to make this work??

    Reply
  • How to navigate between tabs using hot keys

    Posted by Legacy on 04/13/2000 12:00am

    Originally posted by: Akhilesh

    Hi,

    Is there any way to navigate between tabs,
    using Hot keys like Ctrl + Tab.

    Thanks
    Akhilesh

    Reply
  • Problem with Tabs in DLL

    Posted by Legacy on 03/21/2000 12:00am

    Originally posted by: Twist


    I use your CXTabCtrl class, which is really good, but I have a problem to use it into DLL.
    In fact, it's when the application runs the DoDataExchange function, the GetDlgItem doesn't. I tried to use an independant pointer, and not a Claas member of AppWizzard, but the problem is the same : GetDlgItem returns NULL pointer.

    If you got a soution, It would be really great. Otherwise, tell it to me.

    Thanks in advance

    Reply
  • Dialog don't appear in the TabCtrl !!

    Posted by Legacy on 03/15/2000 12:00am

    Originally posted by: cyril

    The Control is really Good, but I have a problem. When I run my program, the Dialog I wanted to put into the TaCtrl appear in the top-left corner of the Screen.

    When I change the tab, the Dialog Changes too !!

    It's like if it was working, but the Dialogs are not in the TabCtrl


    Thanks for your reponse

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds