Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ListBox selection

ListBox selection

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BoudewijnEctor
    wrote on last edited by
    #1

    Hi, I've got a listBox in a program, and an array of instances of an own class. This class has a property called ID, and name. Both can be accessed by using get_ID(), and get_name(). The user must be able to select an instance in the listbox by selcting its name and clicking a button. This works, I use getCurSel, as long as I know what position each instance has in the list. But other people will need to add their own instances, and they will have to change all the ID's etc. Is there a way to determine the text selected in a list box, instead of the position of the selection, so I will know exactly which instance is selected? Thanks,

    H J 2 Replies Last reply
    0
    • B BoudewijnEctor

      Hi, I've got a listBox in a program, and an array of instances of an own class. This class has a property called ID, and name. Both can be accessed by using get_ID(), and get_name(). The user must be able to select an instance in the listbox by selcting its name and clicking a button. This works, I use getCurSel, as long as I know what position each instance has in the list. But other people will need to add their own instances, and they will have to change all the ID's etc. Is there a way to determine the text selected in a list box, instead of the position of the selection, so I will know exactly which instance is selected? Thanks,

      H Offline
      H Offline
      HalfWayMan
      wrote on last edited by
      #2

      The way that I use is when I add a string to the list box, I also setup a pointer to the custom data, in your case, your own class. I usually do this with: m_ListBox.SetItemDataPtr (m_ListBox.AddString (item->name), (void *)item); You can then get the item data for the current selection like so: my_class *item = (my_class *)m_ListBox.GetItemDataPtr (m_ListBox.GetCurSel ()); Does this help? www.originalsinsoftware.com

      1 Reply Last reply
      0
      • B BoudewijnEctor

        Hi, I've got a listBox in a program, and an array of instances of an own class. This class has a property called ID, and name. Both can be accessed by using get_ID(), and get_name(). The user must be able to select an instance in the listbox by selcting its name and clicking a button. This works, I use getCurSel, as long as I know what position each instance has in the list. But other people will need to add their own instances, and they will have to change all the ID's etc. Is there a way to determine the text selected in a list box, instead of the position of the selection, so I will know exactly which instance is selected? Thanks,

        J Offline
        J Offline
        Jagadeesh VN
        wrote on last edited by
        #3

        You can use the CListBox::GetText method. GetText accepts the position and LPSTR as input, and you can pass the selected item position, and get the corresponding text. pListBox->GetText( position, str.GetBuffer(MAX_LENGTH) ); "A robust program is resistant to errors -- it either works correctly, or it does not work at all; whereas a fault tolerant program must actually recover from errors."

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups