添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a problem which is most likely a simple problem, but neverthe less still a problem for me. I am using the Listbox in Win32 / C++ and when getting the selected text from my listbox the string returned is just garbage. It is a handle to a struct or similar?

Below is the code and an example of what I get.

std::string Listbox::GetSelected() {
int index = -1;
int count = 0;
count = SendMessage(control, LB_GETSELCOUNT, 0, 0);
if(count > 0) {
    index = SendMessage(control, LB_GETSEL, 0, 0);
return GetString(index);
std::string Listbox::GetString(int index) {
int count = 0;
int length = 0;
char * text;
if(index >= 0) {
    count = GetItemCount();
    if(index < count) {
        length = SendMessage(control, LB_GETTEXTLEN, (WPARAM)index, 0);
        text = new char[length + 1];
        SendMessage(control, LB_GETTEXT, (WPARAM)index, (LPARAM)text);
std::string s(text);
delete[] text;
return s;

GetItemCount just does that. It just gets the number of items currently in the listbox.

The string I was grabbing from the Listbox is "Test String" and it returned ¨±é» Tzã

Any help is appericated, thanks.

Ok, I narrowed it down to my GetSelected function as GetString returns the correct string.

Also, what is the return of SendMessage and how does it compare to the expected LB messages? – Andrew Grant Apr 10, 2009 at 22:50 Well, GetString returns the proper string but GetSelected returns the garbage string...but GetSelected just returns GetString(index). So it may still be in GetString. – Cory Apr 10, 2009 at 22:56

The LB_GETSEL message does not return the index of a selected item, it returns the selected STATE of the ITEM you pass in WPARAM.

You also have a serious bug where if no items are selected you will attempt to retrieve the string of the item at index -1, which is clearly wrong. Checking the return values of these SendMessage calls would have helped you diagnose the problem.

Here's an example of how to get the text of the first selected item;

// get the number of items in the box.
count = SendMessage(control, LB_GETCOUNT, 0, 0);
int iSelected = -1;
// go through the items and find the first selected one
for (int i = 0; i < count; i++)
  // check if this item is selected or not..
  if (SendMessage(control, LB_GETSEL, i, 0) > 0)
    // yes, we only want the first selected so break.
    iSelected = i;
    break;
// get the text of the selected item
if (iSelected != -1)
  SendMessage(control, LB_GETTEXT, (WPARAM)iSelected , (LPARAM)text);

Alternatively you can use LB_GETSELITEMS to get a list of the items that are selected.

Thank you very much for clearing that up, the changes seem to work great. Currently I check to make sure that the index >= 0 before trying to select a string from the listbox. – Cory Apr 10, 2009 at 23:27

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.