Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initializing an list box in a dialog

180 views
Skip to first unread message

john

unread,
Nov 9, 2010, 6:48:51 PM11/9/10
to
How can I populate a CListBox when I initially display it in a dialog?

I have a dialog box that contains a list box. The dialog box (created
with the resource editor) has an associated CDialog class called
CListDialog, and the list box has a CListBox class called mListBox.

I want to display the dialog box modally when a menu item Dialog/Open
is selected. But I want the list box to be popukated with some
initial strings. So what I thought I could do was something similar
to what I would do with other dialog variables, and I did this:

void CListBoxDialogView::OnDialogOpen()
{
CListDialog dialog;
dialog.mListBox.AddString("foo");
dialog.DoModal();
}

This blows up with a debug assertion failure on the AddString call.
(If I take out the AddString line, the empty list box displays
properly.

Can somebody tell me what I'm doing wrong here? This seems like it
should be straightforward but I'm apparently missing something.
What's the proper way to do this?


-- john
Reply-to address is real

ScottMcP [MVP]

unread,
Nov 9, 2010, 9:00:17 PM11/9/10
to

The dialog, and its listbox control, have not been created yet when
you call DoModal. You have to initialize the listbox in the dialog's
OnInitDialog function. You might want to make a CStringArray member
in the dialog. You can initialize that before DoModal, then copy from
the string array to the listbox in OnInitDialog.

john

unread,
Nov 10, 2010, 8:37:05 PM11/10/10
to


Yes, that did the trick. Thank you, Scott. And thanks, too, for the
string-array suggestion; it probably would have taken me a while to
come up with that idea.

You've steered me (with a little more digging) to what I think is an
understanding of what the problem was: CListBox inherits from CWnd,
and CWnd objects need to have their Create() called before they can be
used for anything -- just constructing them is not enough to make them
usable. Create, I'm guessing, is called within DoModal, and
OnInitDialog gives you a handle to operate on the dialog object after
the Create but before it gets displayed. So I think I'll have a
similar problem (and can use a similar solution) for any dialog item
that derives from CWnd, e.g., CComboBox and CEdit controls. Have I
got that about right?

Thanks again for your help.

Joseph M. Newcomer

unread,
Nov 12, 2010, 7:55:39 PM11/12/10
to
On Tue, 09 Nov 2010 18:48:51 -0500, john <fakea...@nowhere.com> wrote:

>How can I populate a CListBox when I initially display it in a dialog?
>
>I have a dialog box that contains a list box. The dialog box (created
>with the resource editor) has an associated CDialog class called
>CListDialog, and the list box has a CListBox class called mListBox.
>
>I want to display the dialog box modally when a menu item Dialog/Open
>is selected. But I want the list box to be popukated with some
>initial strings. So what I thought I could do was something similar
>to what I would do with other dialog variables, and I did this:
>
>void CListBoxDialogView::OnDialogOpen()
>{
> CListDialog dialog;
> dialog.mListBox.AddString("foo");
> dialog.DoModal();
>}
>
>This blows up with a debug assertion failure on the AddString call.
>(If I take out the AddString line, the empty list box displays
>properly.

****
There IS NO SUCH THING as "a debug assertion". There IS, however, "I got a debug
assertion in the file XXXXX.cpp on line NNNN in Visual Studio version VVVV".

Of course, when you got this debug assertion, the first thing you did was enter the
debugger and find out why, and examine all the relevant variables.

The most likely thing you would have discovered is that mListBox had a NULL CWnd handle,
and this would have happened if you failed to properly call the superclass in
CListBoxDialogView::OnInitDialog.
****


>
>Can somebody tell me what I'm doing wrong here? This seems like it
>should be straightforward but I'm apparently missing something.
>What's the proper way to do this?

****
You failed to give any useful information, but if my guess proves correct, you need to
show us the first few lines of your OnInitDialog handler, the declaration of the variable
mListBox, and the contents of your DoDataExchange handler.
joe
***


>
>
>-- john
>Reply-to address is real

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

john

unread,
Nov 15, 2010, 5:59:57 PM11/15/10
to


Thank you ever so much for the scolding.

Apparently I gave enough useful information for Scott to pinpoint the
problem, steer me to a solution, and even provide an un-asked-for but
helpful hint on how to implement it. With his help it's now working
just fine.

0 new messages