|
-
April 29th, 1999, 11:05 PM
#1
[MFC] A Problem About Data Access Between Two Class
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!
-
April 30th, 1999, 02:06 AM
#2
Re: [MFC] A Problem About Data Access Between Two Class
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
-
April 30th, 1999, 02:15 AM
#3
Re: [MFC] A Problem About Data Access Between Two Class
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
-
April 30th, 1999, 05:16 AM
#4
Re: [MFC] A Problem About Data Access Between Two Class
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
-
April 30th, 1999, 02:13 PM
#5
Re: [MFC] A Problem About Data Access Between Two Class
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|