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
for i = 0 to listbox.items.count -1

if listbox.item(i).selected = true then
  'do something here
end if

next

Open in new window


Of course that code doesn't come close to working.

Thanks...
Steve5140Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Well your previous post confirmed this:  the things in your ListBox are actually DataRowView objects. In my code, the iterator variable item is define to be of type object. This means you don't immediately have access to the members of a DataRowView object. You have to cast item to a DataRowView in order to inspect it:

For Each item in listbox.SelectedItems
    Dim drv As DataRowView = DirectCast(item, DataRowView)
    Dim employeeName As String = drv("EmployeeName").ToString()
Next

Open in new window

0
 
dustockCommented:
Give this a shot


        For Each selectedItem As [Object] In ListBox1.SelectedItems
                'Your Code
        Next

Open in new window

0
 
käµfm³d 👽Commented:
Why not just inspect the SelectedItems collection?

e.g.

For Each item in listbox.SelectedItems
    ' do something here
Next

Open in new window


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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
RimvisCommented:
Hi Steve5140,

Something like this should work:

For Each s As String In listBox1.SelectedItems
    'do something here
    Debug.Print(s)
Next
0
 
Steve5140Author Commented:
The following code returns "System.Data.DataRowView"

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

Open in new window

0
 
Steve5140Author Commented:
if it helps any, this listbox is bound to a data table as such:

  lstEmployees.DataSource = dtEmp
  lstEmployees.DisplayMember = "MatchCode"

Open in new window


the code  Debug.Print(lstEmployees.DisplayMember.ToString) returns "MatchCode"
0
 
Steve5140Author Commented:
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. "
0
 
Steve5140Author Commented:
This line

Debug.Print(lstEmployees.SelectedIndex.ToString)

prints 0 (zero) every time

None of this makes any sense to me...
0
 
Steve5140Author Commented:
Kaufmed,

OK, I've got it working with one minor change

changed

drv("EmployeeName").ToString

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 ?
0
 
YZlatCommented:
to retrieve selected item from a listbox, try using

listbox.text
0
 
käµfm³d 👽Commented:
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 ?
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:

public class Car
{
    private string _make;

    public Car(string make)
    {
        this._make = make;
    }

    public override ToString()
    {
        return this._make;
    }
}

Open in new window


...and you data bound the LB to a list of Car objects, then your LB would show the Make strings within it.
0
 
käµfm³d 👽Commented:
...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.
0
 
YZlatCommented:
try this:

 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

Open in new window


the code above will get all the selected items into an array
0
 
käµfm³d 👽Commented:
@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?
0
 
Steve5140Author Commented:
Kaufmed,

Thanks for the answer, and for the follow up explanation.  This turned out to be more complicated than I ever expected.

-Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.