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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Listbox - How to keep an index

Listbox - How to keep an index

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasedata-structureshelptutorial
9 Posts 5 Posters 0 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.
  • K Offline
    K Offline
    Kreatief
    wrote on last edited by
    #1

    Hi, i am using a listbox with several entries. At runtime I wanna delete an entry without losing the others indizes. The reason is: I am using an array of a structure, and every entry has its own structure. But, if I delete an entry, lets say, at the beginning, all others will change their index. This way I cant use this index as number for the structure array. I tried to do a little workaround with SetItemData, but thats some kinda too complicated. What can I do? Did I miss anything? One more thing, which may solve the problem too: How can i change the text of a Listbox item? I used to delete the item and insert it again,but that causes the same problems with indexing as mentioned above! DKT

    2 M 2 Replies Last reply
    0
    • K Kreatief

      Hi, i am using a listbox with several entries. At runtime I wanna delete an entry without losing the others indizes. The reason is: I am using an array of a structure, and every entry has its own structure. But, if I delete an entry, lets say, at the beginning, all others will change their index. This way I cant use this index as number for the structure array. I tried to do a little workaround with SetItemData, but thats some kinda too complicated. What can I do? Did I miss anything? One more thing, which may solve the problem too: How can i change the text of a Listbox item? I used to delete the item and insert it again,but that causes the same problems with indexing as mentioned above! DKT

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #2

      When deleting an item in listbox why don't you delete the corresponding array item (ie, update you array ) also.
      There is no spoon. suhredayan

      K 1 Reply Last reply
      0
      • 2 224917

        When deleting an item in listbox why don't you delete the corresponding array item (ie, update you array ) also.
        There is no spoon. suhredayan

        K Offline
        K Offline
        Kreatief
        wrote on last edited by
        #3

        Cause, if I delete the corresponding array item, all others will keep their index for the array, not like the listbox. If it would change its index just like the listbox does, there wouldnt be any problem! And I dont think that you can simply change the indizes of a structure array! Correct me if I am wrong. DKT

        2 1 Reply Last reply
        0
        • K Kreatief

          Cause, if I delete the corresponding array item, all others will keep their index for the array, not like the listbox. If it would change its index just like the listbox does, there wouldnt be any problem! And I dont think that you can simply change the indizes of a structure array! Correct me if I am wrong. DKT

          2 Offline
          2 Offline
          224917
          wrote on last edited by
          #4

          Yes, you have to take the pain of removing the corresponding structure from the array and shift all the below structures up and update the array count variable. MFC provide collection class to do this for you CArray, CMap etc. A better solution will be to use CMap and map the structure with the listbox text. But for this to work nicely there should be no repetations in listbox contents.
          There is no spoon. suhredayan

          K 1 Reply Last reply
          0
          • 2 224917

            Yes, you have to take the pain of removing the corresponding structure from the array and shift all the below structures up and update the array count variable. MFC provide collection class to do this for you CArray, CMap etc. A better solution will be to use CMap and map the structure with the listbox text. But for this to work nicely there should be no repetations in listbox contents.
            There is no spoon. suhredayan

            K Offline
            K Offline
            Kreatief
            wrote on last edited by
            #5

            What excatly do you mean by mapping the structure with the listbox text? I will work on that CMap class, and hope to find some useful information. Thanks for pushing me in the right direction, and it would be nice if you could explain me the question above! EDIT: If I am right, this class will connect an ID with another, chosable, ID. That seems to be like the SetItemData function for listbox entries, is that correct? What is more useful? I am using this function right now, and its eehm, yeah, like a little, not that fine, workaround for the problem... DKT

            B 1 Reply Last reply
            0
            • K Kreatief

              Hi, i am using a listbox with several entries. At runtime I wanna delete an entry without losing the others indizes. The reason is: I am using an array of a structure, and every entry has its own structure. But, if I delete an entry, lets say, at the beginning, all others will change their index. This way I cant use this index as number for the structure array. I tried to do a little workaround with SetItemData, but thats some kinda too complicated. What can I do? Did I miss anything? One more thing, which may solve the problem too: How can i change the text of a Listbox item? I used to delete the item and insert it again,but that causes the same problems with indexing as mentioned above! DKT

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              you need to manually synchronize your "internal array" with the listbox. I would suggest that you change your "internal array" to something else, like a vector or a list ( either plain MFC or STL ); those will "compact" themselves if you remove an element; if you remove element 5, there will not be an empty space between 4 and 6 ( something like that ).


              Maximilien Lincourt Your Head A Splode - Strong Bad

              K 1 Reply Last reply
              0
              • K Kreatief

                What excatly do you mean by mapping the structure with the listbox text? I will work on that CMap class, and hope to find some useful information. Thanks for pushing me in the right direction, and it would be nice if you could explain me the question above! EDIT: If I am right, this class will connect an ID with another, chosable, ID. That seems to be like the SetItemData function for listbox entries, is that correct? What is more useful? I am using this function right now, and its eehm, yeah, like a little, not that fine, workaround for the problem... DKT

                B Offline
                B Offline
                BlackDice
                wrote on last edited by
                #7

                what I do is create each structure on the heap, and store its pointer for the itemdata. I also put the pointers in a vector container. this way, I can cast the itemdata of an item back to a pointer of my type of struct or class, and delete items whenever I need to. The only thing is making sure that you have some type of cleanup routine in the class that's using the vector, to iterate through the collection and call 'delete' on each element. If I write code in my sleep, does that make me brilliant, or just a lazy programmer? My articles www.stillwaterexpress.com BlackDice - the programmer formerly known as bdiamond

                D 1 Reply Last reply
                0
                • M Maximilien

                  you need to manually synchronize your "internal array" with the listbox. I would suggest that you change your "internal array" to something else, like a vector or a list ( either plain MFC or STL ); those will "compact" themselves if you remove an element; if you remove element 5, there will not be an empty space between 4 and 6 ( something like that ).


                  Maximilien Lincourt Your Head A Splode - Strong Bad

                  K Offline
                  K Offline
                  Kreatief
                  wrote on last edited by
                  #8

                  If this is true for vectors, then i will give it a try with vectors! I hope this will work better. Thank you. DKT

                  1 Reply Last reply
                  0
                  • B BlackDice

                    what I do is create each structure on the heap, and store its pointer for the itemdata. I also put the pointers in a vector container. this way, I can cast the itemdata of an item back to a pointer of my type of struct or class, and delete items whenever I need to. The only thing is making sure that you have some type of cleanup routine in the class that's using the vector, to iterate through the collection and call 'delete' on each element. If I write code in my sleep, does that make me brilliant, or just a lazy programmer? My articles www.stillwaterexpress.com BlackDice - the programmer formerly known as bdiamond

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    BlackDice wrote: what I do is create each structure on the heap, and store its pointer for the itemdata. I concur. This is just the natural way of dealing with listbox data.


                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    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