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

**URL:** <https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798>\
**Category:** Factual Questions\
**Created:** [March 27, 2004, 9:39pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798 "2004-03-27T21:39:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 9:39pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/1 "2004-03-27T21:39:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![bashere](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bashere](https://boards.straightdope.com/u/bashere)\
**Post date:** [March 27, 2004, 10:12pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/2 "2004-03-27T22:12:42Z")

</div>

> [@Revtim](#):
>
> 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)

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [March 27, 2004, 10:20pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/3 "2004-03-27T22:20:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Finagle](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@Finagle](https://boards.straightdope.com/u/Finagle)\
**Post date:** [March 27, 2004, 10:24pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/4 "2004-03-27T22:24:35Z")

</div>

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](http://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/](http://www.codeguru.com/).

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 10:55pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/5 "2004-03-27T22:55:46Z")

</div>

> [@bashere](#):
>
> 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)

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.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 11:04pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/6 "2004-03-27T23:04:22Z")

</div>

> [@Finagle](#):
>
> 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](http://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/](http://www.codeguru.com/).

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?

---

<div class="post-metadata">

**Author:** ![bashere](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bashere](https://boards.straightdope.com/u/bashere)\
**Post date:** [March 27, 2004, 11:06pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/7 "2004-03-27T23:06:37Z")

</div>

> [@Revtim](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 11:16pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/8 "2004-03-27T23:16:47Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![bashere](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@bashere](https://boards.straightdope.com/u/bashere)\
**Post date:** [March 27, 2004, 11:29pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/9 "2004-03-27T23:29:33Z")

</div>

> [@Revtim](#):
>
> **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.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 11:40pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/10 "2004-03-27T23:40:48Z")

</div>

It sure did **bashere** , thanks again.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [March 27, 2004, 11:56pm UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/11 "2004-03-27T23:56:52Z")

</div>

> [@Khadaji](#):
>
> In addtion to the answer already provided, I’d like to point you at [www.codeguru.com](http://www.codeguru.com) which has some great articles, a message boad for questions and an FAQ.

Thanks, I’ll check that out!

---

<div class="post-metadata">

**Author:** ![erislover](https://avatars.discourse-cdn.com/v4/letter/e/71e660/32.png) [@erislover](https://boards.straightdope.com/u/erislover)\
**Post date:** [March 28, 2004, 12:14am UTC](https://boards.straightdope.com/t/ms-visual-c-how-do-i-reference-use-my-listbox/236798/12 "2004-03-28T00:14:56Z")

</div>

Why aren’t you using **UpdateData(TRUE)**?
