Solved

Assertion error when using CComboBox.AddString() method.

Posted on 2001-06-20
3
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

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