Steve5140
asked on
Need to get the selected items in a listbox
Hello Experts,
I can't believe how much trouble I'm having with this seemly simple problem :(
I have a listbox of employee names.
I want to loop through the list box and take action on each selected name.
** This is a Windows application - VB .net
I just want to do something like
Of course that code doesn't come close to working.
Thanks...
I can't believe how much trouble I'm having with this seemly simple problem :(
I have a listbox of employee names.
I want to loop through the list box and take action on each selected name.
** This is a Windows application - VB .net
I just want to do something like
for i = 0 to listbox.items.count -1
if listbox.item(i).selected = true then
'do something here
end if
next
Of course that code doesn't come close to working.
Thanks...
Why not just inspect the SelectedItems collection?
e.g.
The type of item will be object, but the actual object being referenced will be the type of whatever kinds of things you stuck in the ListBox. You will need to cast item to the actual type to get access to any of that type's properties/methods.
e.g.
For Each item in listbox.SelectedItems
' do something here
Next
The type of item will be object, but the actual object being referenced will be the type of whatever kinds of things you stuck in the ListBox. You will need to cast item to the actual type to get access to any of that type's properties/methods.
Hi Steve5140,
Something like this should work:
For Each s As String In listBox1.SelectedItems
'do something here
Debug.Print(s)
Next
Something like this should work:
For Each s As String In listBox1.SelectedItems
'do something here
Debug.Print(s)
Next
ASKER
The following code returns "System.Data.DataRowView"
Obviously, I want the employee name, which is the string displayed in the list box
Obviously, I want the employee name, which is the string displayed in the list box
For Each item In lstEmployees.SelectedItems
Debug.Print(lstEmployees.SelectedItem.ToString)
Next
ASKER
if it helps any, this listbox is bound to a data table as such:
the code Debug.Print(lstEmployees.D isplayMemb er.ToStrin g) returns "MatchCode"
lstEmployees.DataSource = dtEmp
lstEmployees.DisplayMember = "MatchCode"
the code Debug.Print(lstEmployees.D
ASKER
I've got to be honest, this has me very confused....
"The type of item will be object, but the actual object being referenced will be the type of whatever kinds of things you stuck in the ListBox. You will need to cast item to the actual type to get access to any of that type's properties/methods. "
"The type of item will be object, but the actual object being referenced will be the type of whatever kinds of things you stuck in the ListBox. You will need to cast item to the actual type to get access to any of that type's properties/methods. "
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This line
Debug.Print(lstEmployees.S electedInd ex.ToStrin g)
prints 0 (zero) every time
None of this makes any sense to me...
Debug.Print(lstEmployees.S
prints 0 (zero) every time
None of this makes any sense to me...
ASKER
Kaufmed,
OK, I've got it working with one minor change
changed
drv("EmployeeName").ToStri ng
to
drv("MatchCode").ToString
Now, let me see if I can understand this...
It would appear that since I bound the Listbox to a datatable, what I see displayed are not items of text, but an item in a datarow that is bound to the listbox.
is that correct ?
The lines that you added are required to parse the name field out of the datarow ?
OK, I've got it working with one minor change
changed
drv("EmployeeName").ToStri
to
drv("MatchCode").ToString
Now, let me see if I can understand this...
It would appear that since I bound the Listbox to a datatable, what I see displayed are not items of text, but an item in a datarow that is bound to the listbox.
is that correct ?
The lines that you added are required to parse the name field out of the datarow ?
to retrieve selected item from a listbox, try using
listbox.text
listbox.text
It would appear that since I bound the Listbox to a datatable, what I see displayed are not items of text, but an item in a datarow that is bound to the listbox.More or less. The ListBox will call the default ToString method defined for the types you stick into it. You saw "System.Data.DataRowView" when you called ToString against each selected item. This is because the default ToString method for DataRowView prints its type name (as do most Framework classes). If you had a class like this:
is that correct ?
public class Car
{
private string _make;
public Car(string make)
{
this._make = make;
}
public override ToString()
{
return this._make;
}
}
...and you data bound the LB to a list of Car objects, then your LB would show the Make strings within it.
...that is, unless you override such behavior by using the DisplayMember as you have done. The objects are still DataRowViews (or Cars), but you are designating one particular property to be used as the value displayed in the LB.
try this:
the code above will get all the selected items into an array
Dim arr(listbox.SelectedItems.Count - 1) As String
Dim i As Integer
Dim j As Integer = 0
For i = 0 To listbox.Items.Count - 1
If listbox.GetSelected(i) Then
arr(j) = listbox.GetItemText(listbox.Items(i))
j =j+ 1
End If
Next
the code above will get all the selected items into an array
@YZlat
Isn't it a bit silly to query the SelectedItems' Count property only to later loop through every item in the ListBox? Wouldn't it be more to the point to just loop over the SelectedItems property?
Isn't it a bit silly to query the SelectedItems' Count property only to later loop through every item in the ListBox? Wouldn't it be more to the point to just loop over the SelectedItems property?
ASKER
Kaufmed,
Thanks for the answer, and for the follow up explanation. This turned out to be more complicated than I ever expected.
-Steve
Thanks for the answer, and for the follow up explanation. This turned out to be more complicated than I ever expected.
-Steve
Open in new window