Search code examples
c++tabsmfcctabctrl

Adding tabs to CTabCtrl in MFC


I am trying to add a CTabCtrl into my MFC application. I am trying to follow the MSDN directly.

MSDN Adding Tab

*MSDN: Adding Tabs to Tab Control

Here is what I have tried:

DDX_Control(pDX, TAB1, m_TabCtrl);

TC_ITEM ti;
ti.mask = TCIF_TEXT;
ti.pszText = _T("First Tab");

m_TabCtrl.InsertItem(0,&ti);

I am receiving the following error message: Assertion Fail

If I hit ignore, my CTabCtrl is shown, but without any tabs (just a gray square). If I hit retry, I get the breakpoint at:

_AFXCMN_INLINE BOOL CTabCtrl::SetItem(int nItem, TCITEM* pTabCtrlItem)
{ ASSERT(::IsWindow(m_hWnd)); return (BOOL)::SendMessage(m_hWnd, TCM_SETITEM, nItem, (LPARAM)pTabCtrlItem); }

I have tried adding the header #include "afxcmn.h" but it does not change anything.

I am simply trying to get named tabs to show on my application as a first step. Eventually I wish for the tabs to show modeless dialogs. Can someone tell me what I am doing wrong? Is there a better way to use tabs in MFC?


Solution

  • From your information provided, it's clear that it is ASSERTing on IsWindow(m_hWnd). So that means the window for your tab control has not been created yet when you call InsertItem().

    Are you putting the CTabCtrl in a CDialog derived class or in some other CWnd derived class? Have you set a breakpoint on your DDX_Control() line of code to be sure 1) it is actually being called, and 2) that it is successful? I have a feeling that it is not even being called, because if it was, then you would have a valid m_hWnd, or you would get an ASSERT() at the point of your DDX_Control() call to say that it failed.