[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to get the selected items in a listbox

Posted on 2012-09-05
15
Medium Priority
?
479 Views
Last Modified: 2012-09-06
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...
0
Comment
Question by:Steve5140
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 38369380
Give this a shot


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

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38369388
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
 
LVL 19

Expert Comment

by:Rimvis
ID: 38369410
Hi Steve5140,

Something like this should work:

For Each s As String In listBox1.SelectedItems
    'do something here
    Debug.Print(s)
Next
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Steve5140
ID: 38369458
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
 

Author Comment

by:Steve5140
ID: 38369537
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
 

Author Comment

by:Steve5140
ID: 38369543
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38369643
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
 

Author Comment

by:Steve5140
ID: 38369661
This line

Debug.Print(lstEmployees.SelectedIndex.ToString)

prints 0 (zero) every time

None of this makes any sense to me...
0
 

Author Comment

by:Steve5140
ID: 38369726
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
 
LVL 35

Expert Comment

by:YZlat
ID: 38369759
to retrieve selected item from a listbox, try using

listbox.text
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38370083
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38370100
...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
 
LVL 35

Expert Comment

by:YZlat
ID: 38371858
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38372298
@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
 

Author Closing Comment

by:Steve5140
ID: 38372323
Kaufmed,

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

-Steve
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question