CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 1999
    Posts
    11

    How to delete multiple selection items from a listbox?

    On a form, I have two listbox's. One has items in it, the other is empty. I want to be able to transfer items from one listbox to the other. That part I can do. My problem, is that I want to delete the item, that is being transferd, from it's original listbox. For a single selection, it poses no problem, but if you have a multiple selection enable, it is more complex. I cant use the index after I use the removeitem once, since all the indexes are changed. Example: I have five items (index 0 through 4). I select item 1 & 3. Once I transfer these items to the other listbox, I now want to delete them. If I use the removeitem method once for the item index 1, the other item previously selected (index 3) now becomes index 2! So how can I keep track of all this as I delete items? Is using a while(there are still selected items) conditional loop part of the solution?


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: How to delete multiple selection items from a listbox?

    use a loop like this

    for i = list1.listcount-1 to 0 step -1
    if list1.selected(i) then
    list1.removeitem i
    end if
    next i



    i.e. you loop through the items starting from the end of the list.


  3. #3
    Join Date
    Aug 1999
    Posts
    4

    Re: How to delete multiple selection items from a listbox?

    loop backwards through the list you are deleting from. This way, your indicies will not change.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured