Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

Populating ComboBoxes assertion error

I have a single document form, and I have 2 resource dialogs which I have created.  I have linked them doing the following

void CDAOprac1View::OnButton1()
      // TODO: Add your control notification handler code here
      m_ComboMain.AddString(0,"hi"); //ComboMain is an a member of type CCombo and control
      m_ListMain.AddString("hi"); //ListBox is the same as CCombo, except CListBox type and control.
        Both give me assertion errors.
//      CSubDlg subDlg;

the best way to emulate the problem, is to create new project, single document, database DAO pick any database you have and the rest is abatary, (dynaset, dirty dtect)

Now create the form1 with 2 controls on it
a button: which will popup DIALOG1 and populate Mainfrm's combo and Dialog1's Combo.

a combobox: m_ComboMain which is a control;

in the Dialog1
a button: which will populate the Combobox, (and the MainFrms one.)
a comboBox:  m_ComboDialog1;

Now populate what ever you populate like this
if from form1 to Dialog1, say AddString("Mainfrm to Dialog1")

if from form1 to its own one ("MainFrm to Mainform") in that way.  

Please be a simple as can be.  I get an assertion error doing...

#include "Dialog1.h"
  CDialog1* dlg1;
  dlg1->m_ComboDialog1.AddString("From Mainfrm to Dialog1");
  or this
  CDialog1 dlg1;
  dlg1.m_ComboDialog1.AddString("From ...");


Please any help will be appreciated.

I will increase the points for a working answer.  Please do the emulation.  

Thank you very much in advance.
(I don't really think that a question can be plainer)

sum up.

A simple way to populate ComboDialog1 and ComboMain from both them selves and to each other.

1 Solution
The_BrainAuthor Commented:
Merry Christmas... :)
The_BrainAuthor Commented:
Adjusted points to 30
The_BrainAuthor Commented:
Come on I really need this, it is not for a an assignment so please HELP, this is the 3rd question that I posted that was unanswered. :(
I will increase it even more if you want.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

CDialog1* dlg1;
dlg1->m_ComboDialog1.AddString("From Mainfrm to Dialog1");

CDialog1 dlg1;
dlg1.m_ComboDialog1.AddString("From ...");

Both code sample can't work because the dialog box is not opened (created by Windows).

Use CDialog1::DoModal for a modal dialog box or CDialog1::Create for a non-modal dialog box to open the dialog, and populate your combo box in CDialog1::OnInitDialog method.

The_BrainAuthor Commented:
This is not what I was asking, I can add to the edit box BEFORE the DoModal but I can't pop. the combo box.  that was my question

like this.

Dialog1 *dlg
dlg=new Dialog1;


This works...
but this doesn't work
dlg->Combo1.AddString("hi"); //But I got around the problem anyway,  I created a buffer, I added to the buffer and populated it in the initDialog.  
thanks for a least trying to help :)
Happy new year.
I think you are providing erroneous information to us :
1. The CEdit control does not provide an operator to assign a string value, yet you show in code that you are using one.
2. You are declaring a pointer to a dialog without having initialized it to point at a valid object.

Based on the information you provided, the code is working out of sheer luck or you’ve typed your code incorrectly.

stsanz’s point regarding the dialog not being open is correct, you cannot add values to a common control using MFC when the control doesn’t yet have a valid window handle, check the MFC sources if you don’t believe us.

The method that I would employ to solve this problem is to derive my own combo box class that takes enough arguments in it’s constructor to later populate itself.  Then you can overload the PreSubclassWindow method to insert your population code.

The_BrainAuthor Commented:
Sorry if it seems that I was agressive, I wasn't, I guess my question was not as clear as I thought  here is what I have done.

CEdit control?  (I always use CString m_Edit1)??? is there something that I am missing?)


in my member init.

CString m_Edit1;

CCombo m_Combo1;

later on button1

   Dialog1* Dlg1;
   Dlg1= new Dialog1;
   Dlg1.m_Combo1.AddString("hi"); //assertion error.//


that is what I am doing,  the addstring is actually in a loop which is accessing a database, and that is why I can't do this in the InitDialog1.  (I can't get hold of the Mother Dialogs Member name...  maybe that would be a nicer option.

this-> points to the orignal mother window, but this Obviously doesn't work in the new dialog.  (I need to know the class name.)

some thing to this effect.

MainDialog *Mdlg;
Mdlg=new MainDialog;

Mdlg.m_pSet... etc.

Sorry again for the misunderstanding. :)

  I think you've answered your own problem here to some extent.  If you look at what you are trying to populate (CString, CComboBox) you'll see where you went wrong.  A CString is a member variable which can hold a string but, isn't directly tied to the edit control.  The string isn't displayed until OnInitDialog is called.  The CString is just a place holder for you data.  On the other hand CComboBox is more directly tied to the combobox control.  At least the function you are using.  The AddString function tries to update the combobox by writing the text to its window.  Well obviously there isn't a window available when you do it.  Most likely you will get an assertion error.  The Add function for CComboBox is kinda like the SetWindowText function for an edit control.  Use it at the wrong time and it won't work.

To work around this is very simple.  You need to use a storage class (list class) to store the data in the dialog until OnInitDialog is called.  This is made simple with MFC... providing you with a variety of classes like CObList.  This structure has some nice features and means you don't have to write your own linked list.  Run you loop to get the data from the database and store it in this structure... wait until OnInitDialog and the Add all you want.

There is an alternative if you don't want to use a list class like this.  You can simply pass your CDatabase object or your CRecordset object to the dialog.  In you dialog declare a CDatabase* or a CRecordset* and have a function called SetDatabase to pass the database to you dialog.  Doing this will give you access to the data in the dialog during the OnInitDialog function as well as elsewhere in the dialog.  Below is a code snipet to help out...

void CParentWindow::parentFunction(void)
   CRecrodset someSet;
   CChildDialog dlg;
       AfxMessageBox("Hello World");

void CChildDialog::SetDatabase(CRecordset* pSet)
   m_pChildSet = pSet;   // set the member set to the passed set

void CChildDialog::OnInitDialog()
   // loop here with your database
      comboBox.Add(m_pChildSet->m_SOME_DATA);   // add it to combo
      m_pChildSet->MoveNext();   // move next
The_BrainAuthor Commented:
Thanks, this is Exactly what I did, and yes I do understand now why it gave me the assertion error.  thanks for clearing that up.

Btw, could you send me an e-mail telling me why some people use CEdit and not CString for their Edit boxes?

thanks again.
The_BrainAuthor Commented:
my ICQ no.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now