Solved

Removing multiple string items from a listbox

Posted on 2004-10-15
2
255 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
Comment Utility
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
Comment Utility
Hi Toddhd,

this should work

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

5 Experts available now in Live!

Get 1:1 Help Now