Pladnius Brooks
Pladnius Brooks

Reputation: 1308

C++ MFC - Adding string to combo box prevents window from opening

void _LayersDialog::OnBnClickedRenameGroup()
{
    LOG("CLICKED ON RENAME GROUP\n");
    _LayersNameDialog name_dlg;

    CComboBox* pComboBox = (CComboBox*)name_dlg.GetDlgItem(IDC_LAYERRENAME_COMBO);

    pComboBox->AddString("moose");

    // open the rename window
    if (name_dlg.DoModal() == IDOK)
    {
        LOG("HIT OK");
    }
}

The window refuses to open. If I remove the addstring, it works fine. Any ideas?

Upvotes: 0

Views: 1118

Answers (1)

Scott Jones
Scott Jones

Reputation: 2906

I'm surprised your code does not crash. Until you call DoModal, the dialog does not exist, and nor should any child controls for it. You should initialize child controls in the handler for WM_INITDIALOG (OnInitDialog with an MFC-based dialog).

class _LayersNameDialog : public CDialog
{
    //  See: http://msdn.microsoft.com/en-us/vstudio/fwz35s59(v=vs.110)
    BOOL OnInitDialog() override
    {
        CDialog::OnInitDialog();
        CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_LAYERRENAME_COMBO);
        pComboBox->AddString("moose");
        return TRUE;
    }
};

Upvotes: 8

Related Questions