Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Removing multiple string items from a listbox

Posted on 2004-10-15
2
Medium Priority
?
265 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 200 total points
ID: 12319908
Hi Toddhd,

this should work

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

876 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