0

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?

Community
  • 1
  • 1
Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
  • 1
    Have you clicked Retry to step through the code? – rrirower Mar 11 '15 at 18:12
  • 1
    Clicking Retry on that dialog will lead you more useful debugging info. – Mark Taylor Mar 11 '15 at 18:12
  • 1
    Where in your code is the call to InsertItem? It can't be in DoDataExchange, and it needs to be in a function that is called after the dialog is initialized and control windows are created. – Mark Taylor Mar 11 '15 at 18:14
  • I have updated my question with debugging info. I make the call to InsertItem after `OnInitDialog() {` of my main Dialog's class. – Kyle Williamson Mar 11 '15 at 18:16
  • At the moment, my m_TabCtrl is not trying to link to any dialogs. I am just trying to get the text of each tab to show. – Kyle Williamson Mar 11 '15 at 18:18
  • If what you want is a tabbed looking dialog structure, take a look at CPropertySheet and CPropertyPage. It might be a little easier for you. – rrirower Mar 11 '15 at 18:30
  • 1
    More than likely, you have an invalid handle (m_hwnd) when executing the InsertItem command. That would imply your tab control was not created correctly before you tried to execute the insert command. – rrirower Mar 11 '15 at 18:32

1 Answers1

2

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.

Joseph Willcoxson
  • 5,853
  • 1
  • 15
  • 29