Using the ListBox control in a Windows App

Z

Zack Sessions

Using VB.NET. I have a ListBox control that I have populated with a
list of items. The user selects one or more items from the list to
process. The best way I have found to loop through the selected items
is by:

Dim item as String

For Each item In lbItems.SelectedItems
' process the item
Next

I would like to un-select each list item after it has been processed.
The only way I have found to do that is with the SetSelected method.
But that method required the index of the item to process it. How do I
determine the ListBox index for the selected item?
 
C

Cablewizard

As long as you are happy with unselecting them all at once after you have
processed them, then look at the SelectedIndices property. You will still need
to use the SetSelected method in a loop, but SelectedIndices will return a list
of indexes that are selected instead of the items themselves.
Otherwise, you will be stuck with using a For index instead of a For Each.

Gerald
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Zack Sessions) scripsit:
Using VB.NET. I have a ListBox control that I have populated with a
list of items. The user selects one or more items from the list to
process. The best way I have found to loop through the selected items
is by:

Dim item as String

For Each item In lbItems.SelectedItems
' process the item
Next

I would like to un-select each list item after it has been processed.

\\\
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do While Me.ListBox1.SelectedIndices.Count > 0
Dim i As Integer = Me.ListBox1.SelectedIndices(0)
MsgBox(Me.ListBox1.Items(i))
Me.ListBox1.SetSelected(i, False)
Loop
End Sub
///
 
Z

Zack Sessions

Cablewizard said:
As long as you are happy with unselecting them all at once after you have
processed them, then look at the SelectedIndices property. You will still need
to use the SetSelected method in a loop, but SelectedIndices will return a list
of indexes that are selected instead of the items themselves.
Otherwise, you will be stuck with using a For index instead of a For Each.

Thanks for your response. I hadn't noticed the SelectedIndices method.
I was able to implement what I wanted to do with it with code like the
following:

dim col as Collection = Nothing
dim iIndex as Integer
dim sItem as String
dim bReturn as Boolean

For Each iIndex in myListBox.SelectedIndices
sItem = myListBox.Items.Item(iIndex)
bReturn = Process(sItem)
if bReturn then
if col is nothing then col = new collection
col.Add iIndex
end if
next
if not col is nothing then
For Each iIndex in col
myListBox.SetSelected(iIndex, False)
next
end if

I wanted to deselect each item as it was processed, but if you do, you
change the selected indexes and it screws up the for loop.
 
C

Cablewizard

I wanted to deselect each item as it was processed, but if you do, you
change the selected indexes and it screws up the for loop.

heh, yeah, I forgot to mention that.
hence the example Herfried gave.

Gerald
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top