I've added a combo box to my MFC dialog box. And I've added both a control variable (m_wndImportMode
) and a value variable (m_nImportMode
).
I'm able to use both variables just fine. And I can use the control variable to populate the control just fine. My problem is where is the correct location to populate my control?
If I populate the combo box before DoDataExchange()
then my control variable has not yet been initialized. If I populate the combo box after DoDataExchange()
, then it populates fine but the value does not get set.
BOOL COptionsDlg::OnInitDialog()
{
// If I populate my combo box here,
// my control variable is not yet available
// This will ultimately call DoDataExchange()
CDialog::OnInitDialog();
// If I populate my combo box here,
// DoDataExchange() has already been called and
// so it will not have selected the correct item
// before there were any items
return TRUE; // return TRUE unless you set the focus to a control
}
Playing with this a little more, I can see I can populate the control before calling CDialog::OnInitDialog()
if I use GetDlgItem()
instead of my control variable (which has not yet been initialized), and then the default item will correctly be set as I want. But doesn't the MFC framework provide for populating list controls in a dialog box and still using DoDataExchange()
?
CComboBox
usingAddString()
just fine afterOnInitDialog()
. There is nothing wrong withDoDataExachange
mechanism and MFC framework in general. – Andrew KomiaginDoDataExchange
directly; in the function you are populating the combo you should callUpdateData(FALSE)
after. – sergiolDoDataExchange()
directly either. But I can see inOnInitDialog()
whereCDialog::OnInitDialog()
(whereDoDataExchange()
) is called. And I can populate the control before or after that. And I'm populating it inOnInitDialog()
. Where do you populate it? – Jonathan WoodUpdateData(FALSE)
, to have the framework select the correct item. In casem_nImportMode
happens to get overwritten, you need to save the value across theCDialog::OnInitDialog()
call, restore it when it returns, and callUpdateData(FALSE)
as a final step.UpdateData
works both ways, initializing a dialog box with values from the C++ class, as well as storing dialog entries input by the user back into those values. – IInspectable