473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Using the ListBox control in a Windows App

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
5 7275
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
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
* 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
"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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: A P | last post by:
Hi! I have seen some techniques like this on the web. Currently, I'm using Combo box which values came from database table. One disadvantage is when the combo box have lots of values, users are...
0
by: JMe9ka | last post by:
I have a ListBox to which I am assigning a DataTable as a DataSource, as shown below. ======================================================= DataTable VendorData = new DataTable(); // Code...
5
by: pnp | last post by:
to store other items that just strings? Thanks Peter
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
2
by: Kevin | last post by:
I have a few frame controls on my form in my Windows Forms app. I have a Listbox that stretches over a few of the frame controls but is invisible until the user click a button. The problem is, when...
5
by: markr1000 | last post by:
I must have looked searched in 500+ places that showed up in Google searchs, but not one has an example of what I want to do. I have a Listbox on a User Control because I want to control the...
4
by: kimiraikkonen | last post by:
Hi, I have a odd but a known question about listbox. I know listbox control can provide multi-select, multi-extendend selections. But i wonder if this selection type belongs to them or it has...
3
by: itisthiyagu | last post by:
Hi, Im looking for ListBox which works like Windows-XP's Add/Remove programs listbox. where can i get this kind of listbox ? I want to show a text in ListItem & it should expand with its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.