Confirmation error in dialog manipulation

The name is pretty weak, but I will try to explain here. I am working on an MFC application. When a button is pressed, a new dialog box is created, some lines are added to ComboBoxfrom this second dialog box, and then a dialog is displayed. The code is shown below. Combo1 is a variable CComboBoxdefined in the Class1.h file. The problem is that when I try to call a method dlg.Foo(), the program gives me Debug Assertion Error. The error falls into the line AddString, in addition to the file afxwin2.inlin this line of code:

{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, CB_ADDSTRING, 0, (LPARAM)lpszString); }

CClass1 dlg = new CClass1(this);
dlg.Foo();
dlg.DoModal();

void CClass1::Foo()
{
    Combo1.AddString(TEXT("text"));
}
+3
source share
4 answers

OnInitDialog(), DoModal():

:

class CClass1 : public CDialog
{
    protected:
        virtual BOOL OnInitDialog();
}

cpp:

BOOL CClass1::OnInitDialog()
{
    __super::OnInitDialog();
    Combo1.AddString(TEXT("text"));  // or just call Foo() here if that preferred

    return TRUE;
}
+4

combobox , foo(). , CStringArray CClass1, DDX combobox. DDX .

DDX, :

void AFXAPI 
DDX_CBStringArray (CDataExchange* pDX, int nIDC, CStringArray& strings)
{
    HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
    bool bMine = false;
    CComboBox* pCB = dynamic_cast<CComboBox*>(CWnd::FromHandlePermanent(hWndCtrl));
    if (!pCB)
    {
        pCB = new CComboBox;
        pCB->Attach(hWndCtrl);
        bMine = true;
    }
    if (pDX->m_bSaveAndValidate)
    {
        strings.RemoveAll();
        int nNumStrings = pCB->GetCount();
        CString strVal("");
        for (int x = 0; x < nNumStrings; ++x)
        {
            pCB->GetLBText(x, strVal);
            strings.Add(strVal);
        }
    }
    else
    {
        pCB->ResetContent();
        INT_PTR nSize = strings.GetSize();
        for (INT_PTR x = 0; x < nSize; ++x)
        {
            pCB->AddString(strings.GetAt(x));
        }
    }
    if (bMine)
    {
        pCB->Detach();
        delete pCB;
    }
}

CStringArray CClass1 DDX. , ComboBox, StringArray , , DoModal(). .

CClass1 :

CClass1 : public CDialogEx
{
...
public:
    CStringArray myStringEntries;

...
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
...
};

DoDataExchange :

void CClass1::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CClass1)
    DDX_CBStringArray(pDX, IDC_COMBO1, myStringEntries);
    ...
    //}}AFX_DATA_MAP
}

:

CClass1 dlg(this);
dlg.myStringEntries.Add("Some text");
dlg.myStringEntries.Add("More text");
dlg.DoModal();

, , CClass1:: foo() , CStringArray:

void CClass1::Foo()
{
    myStringEntries.Add(TEXT("text"));
    myStringEntries.Add(TEXT("more text"));
}
+2

! , . , , , combobox DoModal().

0

m_comboBox OnInitDialog, , m_comboBox DoDataExchange.

:

  • OnInitDialog

    CComboBox * cmb = (CComboBox *) GetDlgItem (IDC_YOURCOMBOBOX);

  • or call UpdateDatabefore accessing m_comboBoxwhich will call the function DoDataExchange, as described in the MSDN documentation for the function notes DoDataExchange.

0
source

All Articles