Click to See Complete Forum and Search --> : [MFC] A Problem About Data Access Between Two Class
HelloNet
April 29th, 1999, 11:05 PM
I create a dialog with a ComboBox(CComboBox m_replace). The dialog class name is CAskDlg.
In a member funcion of CView, I add these code:
CAskDlg askDlg;
askDlg.m_replace.AddString("Hello, world.");
askDlg.DoModal();
System show error message (abnormal terminal) while program run to AddString(). But if I put m_replace.AddString() in OnInitDialog(), it works well.
Why that error happens? Thanks!
Rudolf
April 30th, 1999, 02:06 AM
Simply calling CAskDlg askDlg is not enough to get a dialog You can work with. At this time You have a dialog object that is not connected to a window yet. See the CDialog page on the VC help.
In this case the connection is done by DoModal. OnInitDialog is called while DoModal is working so the error doesn't happen.
HTH
Rudolf
Troy T
April 30th, 1999, 02:15 AM
The best way to solve this problem is to have a public accessor function that will actually add whatever data you want to the combo box. Here's an example that takes a CStringArray as a parameter, and adds all the strings to the combo box.
void CAddDlg::AddAllComboBoxEntries(CStringArray& pArray)
{
for( int nIndex = 0; nIndex < pArray.GetCount(); nIndex++ )
{
m_cbxMyCombo.AddString(pArray.GetAt(nIndex));
}
}
Then just fill out a CStringArray like follows:
CStringArray CSA;
CSA.Add( _T("This is a test") );
CSA.Add( _T("This is another test") );
And call your accessor function to add all the entries to the combo:
CAddDlg dlg;
dlg.AddAllComboBoxEntries(CSA);
That should do it! Let me know if you have any questions.
- Troy
Dave Lorde
April 30th, 1999, 05:16 AM
This won't work (did you try it?) - you can't add items to the combobox until its window has been created!
The dialog constructor doesn't create any control windows, that is done by DoModal().
To use a CStringArray initialised outside the dialog, it should be passed to the dialog constructor and stored (as a reference or pointer) for use in OnInitDialog.
Dave
Troy T
April 30th, 1999, 02:13 PM
Yep, I just created a project and tried that approach. It didn't work as you suspected. I did something very similar where I filled data, but I just filled a CStringArray that way, and then used that CStringArray in the OnInitDialog() function once inside the newly spawned dialog. So, my mistake..
- Troy
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.