Listbox - How to keep an index
-
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
-
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
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 -
When deleting an item in listbox why don't you delete the corresponding array item (ie, update you array ) also.
There is no spoon. suhredayanCause, 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
-
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
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 -
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. suhredayanWhat 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
-
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
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
-
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
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
-
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
-
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
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