Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Removing multiple string items from a listbox

Posted on 2004-10-15
2
258 Views
Last Modified: 2010-04-23
Suppose I have ListBox with MultiSelect.Simple set to true. There are several string items in the ListBox, for sake of example, let's say:

ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")

If the user selects Items "One" and "Two" then presses the delete button in the app, I want to remove them without causing an error. But I'm not sure how to do this, and here is why. My first attempt looked something like this:

        Dim x As Integer
        For x = (lbListBox.Items.Count - 1) To 0 Step -1
            If lbListBox.Items(x).Selected = True Then
                lbListBox.Items.RemoveAt(lbListBox.Items.IndexOf(lbListBox.Items(x)))
            End If
        Next

This doesn't work, because the string object doesn't have a .Selected property, so that's no good.

My second attempt looks like this:

        For Each s As String In lbListBox.SelectedItems
            lbListBox.Items.Remove(s)
        Next

This almost works, but since the ListBox resizes dynamically as you go along, this causes an error, hence the need to reverse iterate as you delete (as above). I don't know of a way to do a reverse For-Each, or that would solve the problem.

What I came up with that works, but isn't so pretty, is this:

        Dim al As ArrayList = New ArrayList

        For Each s As String In lbListBox.SelectedItems
            al.Add(s)
        Next

        For i As Integer = al.Count - 1 To 0 Step -1
            lbListBox.Items.RemoveAt(lbListBox.FindString(al(i)))
        Next

Basically, I put each selected item into an ArrayList, that I can then reverse through and delete the items in the ListBox one by one.

Like I said, this works - but is there a more elegant or proper solution?
0
Comment
Question by:toddhd
2 Comments
 
LVL 10

Expert Comment

by:Hans Langer
ID: 12319906
HI,
try this

   For x As Int32 = 0 To ListBox1.SelectedItems.Count - 1
         ListBox1.Items.Remove(ListBox1.SelectedItems.Item(0))
      Next
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 50 total points
ID: 12319908
Hi Toddhd,

this should work

        While lbListBox.SelectedItems.Count > 0
            LbListBox.Items.Remove(lbListBox.SelectedItem)
        End While
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question