By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,476 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,476 IT Pros & Developers. It's quick & easy.

Using the ListBox control in a Windows App

P: n/a
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?
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
There is a "SelectedIndex" property for the list box control. You can try
that...

"Zack Sessions" wrote:
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?

Nov 21 '05 #2

P: n/a
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

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db************************@posting.google.com ...
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?

Nov 21 '05 #3

P: n/a
* zc********@visionair.com (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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

P: n/a
"Cablewizard" <Ca*********@Yahoo.com> wrote in message news:<uK**************@tk2msftngp13.phx.gbl>...
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.

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db************************@posting.google.com ...
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?

Nov 21 '05 #5

P: n/a
> 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

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db*************************@posting.google.co m... "Cablewizard" <Ca*********@Yahoo.com> wrote in message

news:<uK**************@tk2msftngp13.phx.gbl>...
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.

"Zack Sessions" <zc********@visionair.com> wrote in message
news:db************************@posting.google.com ...
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?

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.