How to clear selected items from a list box


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How to clear selected items from a list box

  1. #1
    Join Date
    Sep 2006
    Posts
    3

    How to clear selected items from a list box

    Hi,

    I have a problem clearing the selected items fom a list box (MultiExtended). I have tried with:

    listboxName.SelectedItems.Clear()

    but nothig happens. When I loop and use GetSelected(x) and then RemoveAt(x) if GetSelected(x) = True I can remove selected items but only one by one. Can anybody please explain why listboxName.SelectedItems.Clear() is not working and show me the best and easiest way to clear all items that are selected on a list box by one click on a button?

    Thanking you in advance

    Nesha

  2. #2
    Join Date
    Sep 2006
    Posts
    2
    Mabe there is no better way.

    There is two collection.
    ObjectCollection
    SelectedObjectCollection
    istboxName.SelectedItems.Clear() just Removes all items from SelectedObjectCollection.
    Removing itme from ObjectCollection must use ObjectCollection's methods.

  3. #3
    Join Date
    Sep 2006
    Posts
    3
    Thanks for the reply. You maight be right. I solved the problem the other way, but it just don't make sence: what's the difference between listboxname.Items.Clear() and listboxname.SelectedItems.Clear()?

    Regards

    Nesha

  4. #4
    Join Date
    Dec 2004
    Posts
    163
    Nesha,

    SelectedItems.Clear results in no items being selected.

    Items.Clear results in no items remaining in the listbox.

    To actually remove the selected items from the listbox, you could do something like this:

    For i As Integer = (ListBox1.SelectedItems.Count - 1) To 0 Step -1
    ListBox1.Items.Remove(ListBox1.SelectedItems(i))
    Next

    Kerry Moorman

  5. #5
    Join Date
    Sep 2006
    Posts
    3

    Thumbs up

    Thank you, this clears things. Also, this is much more elegant solution than mine.

    Regards
    Nesha

  6. #6
    Join Date
    Dec 2007
    Posts
    1
    Kerry,

    Ran across that code. Hope you don't mind if I use this for my high school assignment. Much appreciation.

    Croomie

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    In so far as the thread is over a year old, I doubt any objections will be raised.

    In addition, it really wouldn't matter how old it is.....the code is for public use.

    Welcome to DevX

    Do you have any questions on the code?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #8
    Join Date
    Mar 2012
    Posts
    1
    this is the code for clearing the list box just by clicking once:
    ListBox1.items.clear

Similar Threads

  1. Horizontal scroll bar for a list box
    By Anitha in forum Web
    Replies: 1
    Last Post: 05-25-2001, 10:41 PM
  2. Replies: 2
    Last Post: 02-21-2001, 02:13 PM
  3. combo box or list box
    By Grego in forum VB Classic
    Replies: 2
    Last Post: 11-13-2000, 08:51 AM
  4. Replies: 0
    Last Post: 09-18-2000, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center