Link to home
Start Free TrialLog in
Avatar of Steve5140
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
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...
Avatar of dustock
dustock
Flag of United States of America image

Give this a shot


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

Open in new window

Avatar of kaufmed
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.
Hi Steve5140,

Something like this should work:

For Each s As String In listBox1.SelectedItems
    'do something here
    Debug.Print(s)
Next
Avatar of Steve5140
Steve5140

ASKER

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

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"
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. "
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This line

Debug.Print(lstEmployees.SelectedIndex.ToString)

prints 0 (zero) every time

None of this makes any sense to me...
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 ?
to retrieve selected item from a listbox, try using

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.

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.
...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:

 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
@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?
Kaufmed,

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

-Steve