Solved

Assertion error when using CComboBox.AddString() method.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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