Public Class Form1
Private dt As New DataTable
Private loadform As Boolean 'Prevent execution of selectedindexchanged event when loading form
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadform = True
buildtable() 'build a dummy datatable
ListBox1.DataSource = dt
ListBox1.DisplayMember = "LastName"
loadform = False
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If Not loadform Then
Dim dr As DataRowView = DirectCast(ListBox1.SelectedItem, DataRowView)
TextBox1.Text = dr(0)
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
End If
End Sub
Private Sub buildtable()
Dim col1 As New DataColumn("FirstName", GetType(System.String))
Dim col2 As New DataColumn("LastName", GetType(System.String))
Dim col3 As New DataColumn("Phone", GetType(System.String))
dt.Columns.Add(col1)
dt.Columns.Add(col2)
dt.Columns.Add(col3)
For ix As Integer = 0 To 4
Dim nr As DataRow = dt.NewRow
nr(0) = "John" & ix.ToString
nr(1) = "Smith" & ix.ToString
nr(2) = "555-555-000" & ix.ToString
dt.Rows.Add(nr)
Next
End Sub
End Class