Solved

Assertion error when using CComboBox.AddString() method.

Posted on 2001-06-20
3
430 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

705 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

18 Experts available now in Live!

Get 1:1 Help Now