none
Assert failure on add string for CListbox control RRS feed

  • Question

  • I am a newbie. I have created a C++ MFC dialog box where I have edit box (value variable in DDX) and a CListbox (with both control and value variable) associated with it. I want to take the text from edit box and on hitting a button ADD, add the text at the end. I have experimented multiple ways, but I cannot get the text to show in the listbox using m_VALUE_LIST.Insert() or append command. I tried using the c_CONTROL_LISTBOXVAR.AddString() command and it creates an assert failure in winctrl1.cpp on line 185 which is DrawItem(LPDRAWITEMSTRUCT) function. I can get the text to be displayed elsewhere in the dialogbox which means updatedata() and updatedata(false) is working. The function is below with my latest test. I can see the int "I"  to increase by 1 but it does not display the data and also causes an assert failure with or without UpdateData(false).

    void CParticipant::OnBnClickedButtonAddSessions()
    {
     // TODO: Add your control notification handler code here
     UpdateData(true);
     CString str = _T("Item");
     c_SessionsList.AddString(str);
     //c_SessionsList.ResetContent();
     
     int i = c_SessionsList.GetCount();

     //CString str1 = c_SessionsList.Get
     
     //if (m_Sessions.IsEmpty() == FALSE)
     //{
     // if (m_SessionsList.IsEmpty() == TRUE)
     // {
     //  c_SessionsList.AddString(m_Sessions.GetString());
     //  //m_PID.SetString(m_Sessions.GetString());
     // }
     // else
     // {
     //  //int i = c_SessionsList.GetCount();
     //  //c_SessionsList.AddString(m_Sessions.GetString());
     // }
     //}
      UpdateData(false);
    }

    Any help is appreciated because this is getting frustrating. Thanks

    I just updated the code with testing the add string function with a CComboBox and it worked but clicking on the listbox gave the same error, when I clicked retry on the messagebox it gave a different error in afx.h line 317, 

    inline BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)
    {
     // we remove WM_QUIT because if it is in the queue then the message box
     // won't display
     MSG msg;
     BOOL bQuit = PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
     BOOL bResult = _CrtDbgReport(_CRT_ASSERT, lpszFileName, nLine, NULL, NULL);
     if (bQuit)
      PostQuitMessage((int)msg.wParam);
     return bResult;
    }

    Tuesday, June 18, 2019 6:34 PM

All replies

  • I am a newbie. I have created a C++ MFC dialog box where I have edit box (value variable in DDX) and a CListbox (with both control and value variable) associated with it. I want to take the text from edit box and on hitting a button ADD, add the text at the end. I have experimented multiple ways, but I cannot get the text to show in the listbox using m_VALUE_LIST.Insert() or append command. I tried using the c_CONTROL_LISTBOXVAR.AddString() command and it creates an assert failure in winctrl1.cpp on line 185 which is DrawItem(LPDRAWITEMSTRUCT) function. I can get the text to be displayed elsewhere in the dialogbox which means updatedata() and updatedata(false) is working. The function is below with my latest test. I can see the int "I"  to increase by 1 but it does not display the data and also causes an assert failure with or without UpdateData(false).

    void CParticipant::OnBnClickedButtonAddSessions()
    {
     // TODO: Add your control notification handler code here
     UpdateData(true);
     CString str = _T("Item");
     c_SessionsList.AddString(str);
     //c_SessionsList.ResetContent();
     
     int i = c_SessionsList.GetCount();

     //CString str1 = c_SessionsList.Get
     
     //if (m_Sessions.IsEmpty() == FALSE)
     //{
     // if (m_SessionsList.IsEmpty() == TRUE)
     // {
     //  c_SessionsList.AddString(m_Sessions.GetString());
     //  //m_PID.SetString(m_Sessions.GetString());
     // }
     // else
     // {
     //  //int i = c_SessionsList.GetCount();
     //  //c_SessionsList.AddString(m_Sessions.GetString());
     // }
     //}
      UpdateData(false);
    }

    Any help is appreciated because this is getting frustrating. Thanks

    I just updated the code with testing the add string function with a CComboBox and it worked but clicking on the listbox gave the same error, when I clicked retry on the messagebox it gave a different error in afx.h line 317, 

    inline BOOL AFXAPI AfxAssertFailedLine(LPCSTR lpszFileName, int nLine)
    {
     // we remove WM_QUIT because if it is in the queue then the message box
     // won't display
     MSG msg;
     BOOL bQuit = PeekMessage(&msg, NULL, WM_QUIT, WM_QUIT, PM_REMOVE);
     BOOL bResult = _CrtDbgReport(_CRT_ASSERT, lpszFileName, nLine, NULL, NULL);
     if (bQuit)
      PostQuitMessage((int)msg.wParam);
     return bResult;
    }

    A couple of things --

    1) As a "newbie" you should not be using owner draw.  This is implied by the comment that there is an assertion related to the DrawItem function.

    2) The comments about the functions being called don't match the posted code.  For example, the posted code does not contain anything related to m_VALUE_LIST.Insert() or c_CONTROL_LISTBOXVAR.AddString().  So there is no way for us to make sense of this situation.  Obviously, some clarification is needed here.

    3) Use the "Insert Code Block" button when posting code -- it provides a better formatted presentation on the forum.

    Tuesday, June 18, 2019 7:42 PM
  • Hello,

    as RLWA32 already said, your posted code does not match your question. In addition, the code that you show is simply:

    void CParticipant::OnBnClickedButtonAddSessions()
     {
      UpdateData(true);
      CString str = _T("Item");
      c_SessionsList.AddString(str);
      int i = c_SessionsList.GetCount();
      UpdateData(false);
    }

    I think, c_SessionsList is the ListBox Control. But this code doesn't throw an exception. And there is nothing that will be printed on the screen, in an edit control or somewhere else. In addition, UpdateData does not make sense here. You checked the variable i if the string is added to the listbox, so now you know AddString works, and you can delete the line again.

    Please show us the exact code that throws the exception, explain how you inserted the listbox into the dialog, and explain the listbox properties that you changed.

    Regards, Guido


    Wednesday, June 19, 2019 6:43 AM
  • Hello,

    Thank you for posting here.

    If you just want to implement the function which you said, it seems that the following code is enough.

    CString data;
    m_edit.GetWindowText(data);
    m_listbox->AddString(data);

    From the error location you described, it seems to have no connection with the code you posted. Can you post your code about the creation and drawing of listbox? I think that's related to your errors.

    Best Regards,

    Suarez Zhou


    • Edited by Suarez-Zhou Wednesday, June 19, 2019 8:27 AM
    Wednesday, June 19, 2019 8:26 AM