Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3328
  • Last Modified:

CTabView (MFC - VS 2008) - Adding a view at runtime

Hi,

I have an MDI application that creates 3 views and adds it to a CTabView derived class in the OnCreate handler like this:
AddView (RUNTIME_CLASS (CView1), _T("Simple"), 100);
This works fine. Now, I have a menu item on the CChildFrame class from which I want to add another CScrollView derived class to the TabView. Code pasted in the Code section. I get an ASSERT in the AddView function call. Please let me know how to attach a new CView derived class to the CTabView at runtime. I have attached relevant source files after renaming them as text files.

Thanks
Balaji
void CChildFrame::OnViewAddview()
{
CView* pChildView = GetActiveView();
CMFCTabCtrl* pCtrl = (CMFCTabCtrl*)pChildView->GetParent();
CTabbedViewView* pView = (CTabbedViewView*)pCtrl->GetParent();
pView->AddView(RUNTIME_CLASS (CView4), _T("ScrollView"), 103); //<----error here...
}

Open in new window

TabbedView.zip
0
BalajiShankar
Asked:
BalajiShankar
  • 2
1 Solution
 
ZoppoCommented:
Hi BalajiShankar,

which ASSERT is it from within AddView? It may be the creation of a CView4 fails. Could you post code how it is declared?

ZOPPO
0
 
BalajiShankarAuthor Commented:
When I add the new View, the View's OnInitialUpdate does not get called, hence it ASSERTS.

I have attached all the headers and source files, renamed as txt files. Can you please review them and let me know if I am missing something?

Thanks
Balaji
0
 
alb66Commented:
It seems to me that CTabView is not designed to add a view at run time.

Try to modify CTabView::AddView in your derived class adding a parameter:


void CChildFrame::OnViewAddview()
{
	// TODO: Add your command handler code here
	CView* pChildView = GetActiveView();
	CMFCTabCtrl* pCtrl = (CMFCTabCtrl*)pChildView->GetParent();
	CTabbedViewView* pView = (CTabbedViewView*)pCtrl->GetParent();
	pView->AddView(RUNTIME_CLASS (CView4), _T("ScrollView"), 103, NULL, true); <--- call the function with true as 5th parameter
}
 
 
 
int CTabbedViewView::AddView(CRuntimeClass* pViewClass, const CString& strViewLabel, int iIndex /*= -1*/, CCreateContext* pContext/* = NULL*/, bool bAfterCreation )
{
	ASSERT_VALID(this);
	ENSURE(pViewClass != NULL);
	ENSURE(pViewClass->IsDerivedFrom(RUNTIME_CLASS(CView)));
 
	CView* pView = DYNAMIC_DOWNCAST(CView, pViewClass->CreateObject());
	ASSERT_VALID(pView);
 
	if (!pView->Create(NULL, _T(""), WS_CHILD | WS_VISIBLE, CRect(0, 0, 0, 0), &m_wndTabs, (UINT) -1, pContext))
	{
		TRACE1("CTabView:Failed to create view '%s'\n", pViewClass->m_lpszClassName);
		return -1;
	}
 
	CDocument* pDoc = GetDocument();
	if (pDoc != NULL)
	{
		ASSERT_VALID(pDoc);
 
		BOOL bFound = FALSE;
		for (POSITION pos = pDoc->GetFirstViewPosition(); !bFound && pos != NULL;)
		{
			if (pDoc->GetNextView(pos) == pView)
			{
				bFound = TRUE;
			}
		}
 
		if (!bFound)
		{
			pDoc->AddView(pView);
		}
	}
 
	if ( bAfterCreation )
	{
		pView->SendMessage( WM_INITIALUPDATE, 0, 0 );  <--- call oninitialupdate manually
	}
 
	m_wndTabs.InsertTab(pView, strViewLabel, iIndex);
 
	int nTabs = m_wndTabs.GetTabsNum();
	return nTabs - 1;
}

Open in new window

0
 
BalajiShankarAuthor Commented:
Thanks a lot for the solution. It works great!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now