Solved

Removing multiple string items from a listbox

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now