Reputation: 13
I'm using MFC to build an application with two dialogs. When I press a button in the parent dialog a new window including a Combobox should apprear.
I created a first dialog with a button "New". This button will open the second dialog.
Therefor I created a second dialog with a Combobox. The Combobox has a linked variable variableCombobox
. The second class is called CSecond
.
Before I do anything in the new dialog I want to add an item to the Combobox. In the first dialog class I create the new window like this:
void CFirstDlg::OnBnClickedNew()
{
CSecond dlg2 = new CSecond();
dlg2.variableCombobox.AddString(L"test");
dlg2.DoModal();
}
The program crashes in the line I want to add the test
string to the Combobox showing an assertion error.
I noticed that the dlg2
object is null
but I don't know why.
Can anyone tell me how to create a second window immediately adding an new item in the Combobox of the second window?
Upvotes: 1
Views: 2345
Reputation: 16318
The problem is the second dialog is a modal dialog. The windows does not exist before the call to DoModal()
and no longer exist after that function returns. Therefore calling AddString
on the combobox is not all right, since the combo box does not exist at that time.
The solution is to initialize the dialog with your desired values (like in the constructor for instance, or other methods) and then in OnInitDialog()
use those values to setup the controls (including this call to AddString
for the combobox).
Upvotes: 1