Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 170693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:51:16+00:00 2026-05-11T12:51:16+00:00

I have a problem which is most likely a simple problem, but neverthe less

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:51:16+00:00Added an answer on May 11, 2026 at 12:51 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 100k
  • Answers 100k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It won't matter how long you tell it to stay… May 11, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer When you look at the history of a file you… May 11, 2026 at 7:55 pm
  • Editorial Team
    Editorial Team added an answer I would suggest taking a look at the source code… May 11, 2026 at 7:55 pm

Related Questions

Seems like a simple problem: I have an SVN repo inside our firewall. I
I am designing a table in the database which will store log entries from
I have a few classes, such as those that outline database table structure or
I've written a PHP script that handles file downloads, determining which file is being

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.