0
votes

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?

1
Have you clicked Retry to step through the code?rrirower
Clicking Retry on that dialog will lead you more useful debugging info.Mark Taylor
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
I have updated my question with debugging info. I make the call to InsertItem after OnInitDialog() { of my main Dialog's class.Kyle Williamson
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

1 Answers

2
votes

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.