CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Hybrid View

  1. #1
    Join Date
    Mar 2006
    Posts
    63

    [RESOLVED] CDialog:ListBox in Doc/View

    Instantiating a dialog in a Doc function, I can update an Edit box in the dialog, but when I try to use AddString on a Listbox (using "simple text"), I get an Assertion Failure.
    Using VC++6; to illustrate the problem in the simplest app, I created an SDI Doc/View app, added a Dialog class, added a new menu item to invoke the dialog, then added an Editbox (with member variable)and a CListbox (with control member variable) to the dialog. The fact that I can write to the Editbox suggests that the invocation of the Dialog is ok, but the Assertion Error for the List box is a puzzle. I can add data using AddString in an InitDialog function, but this is useless, since I want to add data to the ListBox at runtime. Must be a simple omission somewhere........

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CDialog:ListBox in Doc/View

    More than likely you're calling AddString before the control has been created or after it's been destroyed. What is the ASSERT code telling you?

  3. #3
    Join Date
    Mar 2006
    Posts
    63

    Re: CDialog:ListBox in Doc/View

    Debug assertion failure message shows:

    File: afxwin2.inl
    Line: 669

    In the .inl file at Line 668/669 is:
    [668] _AFXWIN_INLINE int CListBox::AddString(LPCTSTR lpszItem)
    [669] { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, LB_ADDSTRING, 0, (LPARAM)lpszItem); }

    As I wrote earlier, a statement which writes to an Edit Box on the Dialog form is successful, whereas a trying to AddString to the List Box at the same point in the function which instantiates the Dialog causes the Assertion failure. Looks to me like the CListBox::AddString command is expecting to find the Dialog in a standard place, rather than actually getting it from the instantiated Dlg. (Excuse my poor jargonese).

  4. #4
    Join Date
    Jun 2006
    Posts
    645

    Re: CDialog:ListBox in Doc/View

    May be you are missing DDX entry for the control or string. Please check if the variable name is present in your MESSAGE_MAP macro. Also, see to it that OnInitialUpdate() function calls the base class version in your view class.
    Regards,
    Bhushan

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: CDialog:ListBox in Doc/View

    That supports what I said in post #2. The Listbox window doesn't exist at the point you're trying to add strings to it.

  6. #6
    Join Date
    Mar 2006
    Posts
    63

    Re: CDialog:ListBox in Doc/View

    Thanks for reply, bhushan.
    The Dialog class has a DDX entry for the ListBox (c_list):
    void CDlg:oDataExchange(CDataExchange* pDX)
    {
    CDialog:oDataExchange(pDX);
    //{{AFX_DATA_MAP(CDlg)
    DDX_Control(pDX, IDC_LIST1, c_list);
    DDX_Text(pDX, IDC_EDIT1, m_edit1);
    //}}AFX_DATA_MAP
    }
    The MESSAGE_MAP for the entire Dialog class is empty. I would never guess in a million years what to put there, nor why.
    I also cannot guess what to put in View::OnInitialUpdate.
    This c**p drives me mad.

  7. #7
    Join Date
    Mar 2006
    Posts
    63

    Re: CDialog:ListBox in Doc/View

    I should add that I want to access the ListBox from the Doc: that's where the data is.
    I'll also repeat the point: I can write to the EditBox in the dialog from a Doc function, but attempting to access the CListBox of the Dialog causes an assertion error. It is clear that the Doc class doesn't have a clue about the CListBox, but how to get round this? Or any suggestions as to how to present the user with a list of runtime-generated items, from which they can select any or all? Where in any MS info is there any reference to the need to hand crank a CListBox into life (other than when it's embedded in a CDialog-based appplication)? Maybe I need to use the dreaded GetDlgItem(..) ?

  8. #8
    Join Date
    Mar 2008
    Location
    Turin / Italy
    Posts
    178

    Re: CDialog:ListBox in Doc/View

    By moving the
    Code:
    m_list.AddString(m_pDocument->GetEditString());
    after
    Code:
    CDialog::OnInitDialog();
    everything works. Just as GCDEF says. The list control hasn't been initialized(created) that's why you're getting ASSERT on IsWindow.
    Or maybe I haven't understood where the problem stands.
    Last edited by SkyNetTo; August 16th, 2009 at 04:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured