473,606 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Selecte dItems
' 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 7289
There is a "SelectedIn dex" 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.Selecte dItems
' 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********@vis ionair.com> wrote in message
news:db******** *************** *@posting.googl e.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.Selecte dItems
' 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********@visi onair.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.Selecte dItems
' process the item
Next

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


\\\
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
Do While Me.ListBox1.Sel ectedIndices.Co unt > 0
Dim i As Integer = Me.ListBox1.Sel ectedIndices(0)
MsgBox(Me.ListB ox1.Items(i))
Me.ListBox1.Set Selected(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
"Cablewizar d" <Ca*********@Ya hoo.com> wrote in message news:<uK******* *******@tk2msft ngp13.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.Selec tedIndices
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.SetSe lected(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********@vis ionair.com> wrote in message
news:db******** *************** *@posting.googl e.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.Selecte dItems
' 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********@vis ionair.com> wrote in message
news:db******** *************** **@posting.goog le.com... "Cablewizar d" <Ca*********@Ya hoo.com> wrote in message

news:<uK******* *******@tk2msft ngp13.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.Selec tedIndices
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.SetSe lected(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********@vis ionair.com> wrote in message
news:db******** *************** *@posting.googl e.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.Selecte dItems
' 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
2704
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 complaining since you cannot use keyboard to search the value that is needed. Hope you might help me. regards, Me
0
2136
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 here retrieves Data and populates // the VendorData DataTable.
5
4598
by: pnp | last post by:
to store other items that just strings? Thanks Peter
3
4239
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
8
2875
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 this listbox but I can't for the life of me figure out how to draw the control inside ListBox... I get as far as: private void lbImageList_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
7
4526
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 ASP.Net stuff when I need a Windows Form control. I've seen dual listbox populators in countless Windows applications, and have seen them run very fast, so I figured this would be extremely popular. Here's how it should work:
2
1131
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 I try to put the Listbox over the frames in the designer view, the Listbox becomes a member of one of the frames and shows only a tiny bit of the Listbox. Can I prevent the frame from taking the Listbox?
5
7419
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 visibility and/or presence of the Listbox and associated label, and I want to encapsulate some of associated code. I want to be able to pass the collection of strings that will fill the Listbox to the User Control and have it pass on the collection...
4
1439
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 another tecnique. As you may know, in softwares like Windows Media Player or Winamp playlist has a listbox and it can be selected in 2 types. First type refers to player's own selection and second selection type can be done by user to look for...
3
1573
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 description when user selects a particular item. Also i want to add a button into to ListItem. it is same like Add/Remove programs ListBox. anybody help me. -Thiyagu
0
8024
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7959
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8105
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8310
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6781
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5466
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3942
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3987
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.