Solved

Populating ComboBoxes assertion error

Posted on 1998-12-24
10
376 Views
Last Modified: 2013-11-20
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"
.
.
.
onbutton1
{
  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.

0
Comment
Question by:The_Brain
10 Comments
 
LVL 1

Author Comment

by:The_Brain
ID: 1326675
Merry Christmas... :)
0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326676
Adjusted points to 30
0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326677
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.
0
 
LVL 6

Expert Comment

by:stsanz
ID: 1326678
CDialog1* dlg1;
dlg1->m_ComboDialog1.AddString("From Mainfrm to Dialog1");

or
   
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.

0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326679
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;

dlg->Edit1="Hello";

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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Expert Comment

by:rayb
ID: 1326680
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.


0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326681
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?)

any

in my member init.

CString m_Edit1;



CCombo m_Combo1;

later on button1

$$#@%!@!fga::OnButton1
{
   Dialog1* Dlg1;
   Dlg1= new Dialog1;
   
   Dlg1.m_Edit1="Hi";
   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. :)
0
 
LVL 1

Accepted Solution

by:
perrizo earned 50 total points
ID: 1326682
Hi,

  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;
   someSet.Open();
   
   dlg.SetDatabase(&someSet);
   if(dlg.DoModal())
   {
       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
   while(!m_pChildSet->IsEOF())
   {
      comboBox.Add(m_pChildSet->m_SOME_DATA);   // add it to combo
      m_pChildSet->MoveNext();   // move next
   }
}
0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326683
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?

zd13@pipex.co.za

thanks again.
0
 
LVL 1

Author Comment

by:The_Brain
ID: 1326684
my ICQ no.
24538167
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

23 Experts available now in Live!

Get 1:1 Help Now