Solved

Assertion error when using CComboBox.AddString() method.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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