MS Visual C++: How do I reference/use my ListBox?

I’m using Visual Studio .Net to make a simple MFC C++ app.

I added a listbox to the dialog, and according to the properties, its ID is ‘IDC_LIST4’.

How do I figure out what is the name of the object, in the C++ code, that is the listbox? Something like ListBox4 I suppose, but how do precisely find out what it is, so I can add items to the damn thing with its methods?

Assuming you are calling this from inside of the CWnd (or class derived there from):

/* N.B. change the constant to something clearer before another programmer // finds it and mocks you /
CListBox
pListBox = dynamic_cast<CListBox*>(GetDlgItem(IDC_LIST4));

// hopefully, you read the documentation about dynamic_cast, and turned on RunTime Type Info, so you can do this:

ASSERT(pListBox);

if you aren’t, you need to replace the GetDlgItem above with

CListBox* pListBox = dynamic_cast<CListBox*>(::GetDlgItem(hwndParentWindow, IDC_LIST4));

// then call its methods, e.g,

int nCurSel = pListBox->GetCurSel();

// (use of microsoftish hungarian notation is entirely your call; force of habit for me I’m afraid)

In addtion to the answer already provided, I’d like to point you at www.codeguru.com which has some great articles, a message boad for questions and an FAQ.

Are you using the ClassWizard? If so, it pretty much does all the mapping for you. Otherwise, the syntax (at least pre .NET) fairly obscure. I always access the control using the DoDataExchange declaration, although I suspect there’s a load resource command where you can access the handle directly.

As a general Microsoft C++ programming technique, you really can’t beat dejanews (www.deja.com) for questions on “how to”. No matter what you’re trying to do, someone else has previously found the documentation inpenetrable and has asked the question on one of the programming newsgroups. Another tremendously useful site is http://www.codeguru.com/.

Thank you so much for your help.

Unfortunately,
CListBox* pListBox = dynamic_cast<CListBox*>(GetDlgItem(IDC_LIST4));
returns zero, even though I turned on “Enable Run Time Type Info”.

And when I use
CListBox* pListBox = dynamic_cast<CListBox*>(::GetDlgItem(hwndParentWindow, IDC_LIST4));

I get a compliation error: “hwndParentWindow” = undeclared identifier.

I used ClassWizard ages ago with Visual C++ 5.0, and I’m not sure how to do it with .net (my program is straight C++, and not managed code, FWIW).

I tried right-clicking the list box, and choosing 'Add Class", and a window pops up that even says “MFC Class Wizard”. But it doesn’t seem to be making an object that has any relation to the IDC_LIST4 List Box.

Is there some other way to run the Class Wizard?

Hmm…that’s odd. I assume you are calling (GetDlgItem(IDC_LIST4) from a method inside the class that owns the window, right? GetDlgItem(int) only exists inside the CWnd class, so you’ve got to be calling it from inside the window that owns it.

Here is a little more detail. You have, I assume, created a dialog using the resource editor. You have create a class derived from CWnd to handle that class. You have, again using the resource editor, added a ListBox (and you promise it is a list box, right) to the form. If any of those assumptions are incorrect, please let me know.

The first thing to do is see which part is falling: the GetDlgItem() part or the downcast. Try doing this:

CWnd* pListWnd = GetDlgItem(IDC_LIST4);

// check for NULL

ASSERT(pListWnd);

// CListBoxes derive from CWnds, so we can downcast the pointer to get a pointer to the listbox, and then use its methods
CListBox* pListBox = dynamic_cast<CListBox*>(pListWnd);

ASSERT(pListBox);

If the first ASSERT fails, it means that the CWnd you are currently writting cannot find the resource with an ID of IDC_LIST4. My only guesses are that the ListBox actually belongs to another window, or that the ID has changed somehow.

If the second assert fails, it is because the ListBox is really something else.

bashere, it works fine when it’s separated out like that, neither assert is failing and I can indeed send text into ListBox!!!

Weird, huh?

Thanks again for your assitance.

Well, I just searched all of my code for CListBox, and it turns out I separate them (and break the ASSERTS out on separate lines, so I know which failed). I am vaguely embarrassed to admit that I don’t know why my first example didn’t work. That said, hoep this advances the project you’re working on.

It sure did bashere, thanks again.

Thanks, I’ll check that out!

Why aren’t you using UpdateData(TRUE)?