Solved

Removing multiple string items from a listbox

Posted on 2004-10-15
2
257 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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