Solved

Assertion error when using CComboBox.AddString() method.

Posted on 2001-06-20
3
438 Views
Last Modified: 2013-11-20
I have two Dialog frames and I am trying to populate a
CComboBox in the child dialog with elements from a CComboBox from the parent dialog. When I use the AddString
method when loading my child combobox I get an assertion
error at runtime in afwin2.nll at line 735 which seems to check if
the CComboBox is of the CWind class.

Here is the code I use in the parent to call the child:

// Create instance of child dialog
CAddEditDlg * AddProductDialog = new CAddEditDlg( this );

// This works ok so I know all attributes for
// child class have been created by now.
AddProductDialog->SetTitle("Add Product");

// Call method to load the child combobox passing the
// parent's filled combobox.
AddProductDialog->FillProductTypeComboBox
                  ( m_main_combo_control );

int nButtonClicked = AddProductDialog->DoModal();

Here is the method in the child class:

void CAddEditDlg::FillProductTypeComboBox
                  ( CComboBox & comboRef )
{
CString buffer;

for( int i=0; i < comboRef.GetCount(); i++ )
{
comboRef.GetLBText( i, buffer );

// This prints with the correct content
// for buffer.
AfxMessageBox("Adding " + buffer );

// Here is where the assertion error appears.
// I have tried even hard coding a string
// literal like "hello" and I still get the error.
m_combo_control.AddString( (LPCTSTR) buffer );
}

m_combo_control.SetCurSel( 0 );
}

Any help would be greatly appreciated ...

Thanks,
E.E.
 
0
Comment
Question by:eexposito
3 Comments
 
LVL 8

Accepted Solution

by:
VinExpert earned 75 total points
ID: 6212866
Hi,

U r calling AddString function of CComboBox before it has been created!!! so HWND will be NULL. To avoid that. Dont call ur AddProductDialog->FillProductTypeComboBox
( m_main_combo_control ); function from the parent. Instead, map WM_INITDIALOG to ur child dialog and in that function after the base class OnInitDialog() call, call ur function FillProductTypeComboBox(...) there. That should solve ur prob.

Try it out.
VinExpert

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6213449
>>AddProductDialog->FillProductTypeComboBox
                 ( m_main_combo_control );

>>int nButtonClicked = AddProductDialog->DoModal();

u need to switch the order of those two line codes and change its syntax a little bit.
u send a reference control to a dialog which wasn't been created yet.
since u use modal dialog and not modaless
(using ->Create) the assertion took place.

>>CAddEditDlg * AddProductDialog = new CAddEditDlg( this );

- since u send "this" pointer here, i believe that u can use this pointer to get to the m_main_combo_control which is a data member.

briefly, u need to save to this pointer as follow:
CAddEditDlg::CAddEditDlg(CParentDlg* pDlg)
{
m_parentDlg = pDlg;
}

now u can use parentDlg pointer to fill the combo box as follow:

CString buffer;

for( int i=0; i < comboRef.GetCount(); i++ )
{
comboRef.GetLBText( i, buffer );

// This prints with the correct content
// for buffer.
AfxMessageBox("Adding " + buffer );

// Here is where the assertion error appears.
// I have tried even hard coding a string
// literal like "hello" and I still get the error.
m_parentDlg->m_main_combo_control.
.AddString( (LPCTSTR) buffer );
}

m_parentDlg->m_main_combo_control.
.SetCurSel( 0 );


good luck
0
 
LVL 2

Expert Comment

by:Chandra V
ID: 6213840
The problem is adding the items before creating the window

You can simply do the following:

As you are passing the this pointer to the dialog
-> CAddEditDlg * AddProductDialog = new CAddEditDlg( this );

Add a member variable pointer of type parent class ( i.e, suppose - CMainFrame* pFrame;) then in your constructor of CAddEditDlg equate the parameter passed to the member declared (i.e, pFrame = pParent).

Then in the on init dialog do the same as you are doing in the 'FillProductType' combobox by just replacing m_combo_control with pFrame->m_combo_control, and don'comment out the call in the parent

// Call method to load the child combobox passing the
// parent's filled combobox.
//AddProductDialog->FillProductTypeComboBox
                 ( m_main_combo_control );
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xyBalance chalenge 58 88
Expand macro to ask for filename column 8 37
Whole sheet autoscrub still needed 19 48
ShiftLeft challenge 21 76
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now