Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27367

[RESOLVED] Loop Listbox that was populated with it's DataSource and DisplayMember properties?

$
0
0
VB.ET, 2012, framework 3.5
How does one loop through the items in a Listbox when the Listbox has been populated using it's DataSource and DisplayMemebr properties?

In the example below, the upper loop does not work. The Debug line writes out "ListboxTest.Form1+Person". I need the items like "Bob" and "Paul"

In the example below, the lower loop works correctly. How can one write the upper loop to give the same result as lower loop?


Code:

Public Class Form1

    Private People As New List(Of Person)

    Private Class Person
        Public Property Name As String
    End Class

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        People.Add(New Person With {.Name = "Bob"})
        People.Add(New Person With {.Name = "Paul"})

        Me.ListBox1.DataSource = People
        Me.ListBox1.DisplayMember = "Name"

        ' loop below writes out "ListboxTest.Form1+Person" instead of the "Bob"
        ' how does one iterate a Listbox that has been populated using it's DataSource and DisplayMember properties?
        For i As Integer = 0 To Me.ListBox1.Items.Count - 1
            Debug.WriteLine(Me.ListBox1.Items(i))
        Next


        Me.ListBox2.Items.Add("Bob")
        Me.ListBox2.Items.Add("Paul")
        ' loop below works as expected
        For i As Integer = 0 To Me.ListBox2.Items.Count - 1
            Debug.WriteLine(Me.ListBox2.Items(i)) ' need the loop above to work like this?
        Next

    End Sub
End Class


Viewing all articles
Browse latest Browse all 27367

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>