0
votes

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()?

1
Post your code. You should be able to populate CComboBox using AddString() just fine after OnInitDialog(). There is nothing wrong with DoDataExachange mechanism and MFC framework in general.Andrew Komiagin
I've used this for years and had none of your problems. I never call DoDataExchange directly; in the function you are populating the combo you should call UpdateData(FALSE) after.sergiol
@sergiol: I've never called DoDataExchange() directly either. But I can see in OnInitDialog() where CDialog::OnInitDialog() (where DoDataExchange()) is called. And I can populate the control before or after that. And I'm populating it in OnInitDialog(). Where do you populate it?Jonathan Wood
@AndrewKomiagin: Guess I wasn't clear enough. I've fleshed out my description of the issue.Jonathan Wood
After populating your combo box, you need to call UpdateData(FALSE), to have the framework select the correct item. In case m_nImportMode happens to get overwritten, you need to save the value across the CDialog::OnInitDialog() call, restore it when it returns, and call UpdateData(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

1 Answers

0
votes

I resolved this by using GetDlgItem() to retrieve the combo box, and then populating it before CDialog::OnInitDialog() is called. This works as intended.

I'm not sure what I might be doing differently if this is not an issue for anyone else.