0
votes

I'm writing application in vb.NET Visual Studio 2010. I have a table with data stored in 4 columns (“index”, “description”, “value1”, “value2”).
I add “description” column into the listbox1 and I want to show other data from columns “value1” and “value2” in their own textboxes after the text from ListBox1 is chosen.
For example: if I choose “a” from ListBox1, I want to assign to this item a value1= 5 and value2= 300 and this values should appear in TextBox1 and TextBox2, when I choose "b" values 7 and 400 should appear in these textboxes. But I don't know how to do this so I’m asking for help . This is my code:

    Public Class Form1
    Dim dt2 As DataTable = New DataTable
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As 
    system.EventArgs) Handles MyBase.Load
            Dim i As Integer
    dt2.Columns.Add("index ", GetType(Integer))
    dt2.Columns.Add("description", GetType(String))
    dt2.Columns.Add("value1", GetType(Double))
    dt2.Columns.Add("value2", GetType(Double))

    dt2.Rows.Add(New Object() {1, "a", 5, 300})
    dt2.Rows.Add(New Object() {2, "b", 7, 400})
    dt2.Rows.Add(New Object() {3, "c", 8, 500})
    dt2.Rows.Add(New Object() {4, "d", 9, 600})

    If ListBox1.Text = "" Then
        For i = 0 To 3
            ListBox1.Items.Add(dt2.Rows(i).Item("description"))
            AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
            TextBox2.Text = dt2.Rows(i).Item("value1")
        Next
    End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
    For i = 0 To 3
        TextBox2.Text = dt2.Rows.Item(i)("value1")
    Next
End Sub

End Class

1

1 Answers

0
votes

Try this.

  1. Consider clearing the listbox items instead of doing If ListBox1.Text = ""
  2. Do not loop 0 to 3 times, loop based on row count of DataTable.
  3. Do not add the event handler in the loop, do it one time.
  4. Do not loop in SelectedIndexChanged, you will end up with the value of the last loop in the textbox. Use the selected index of the listbox to get the row you want.
  5. Consider using Data Binding instead of adding Items to the ListBox (not demonstrated below)

    Public Class Form1
    
        Dim dt2 As DataTable = New DataTable
    
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles MyBase.Load
    
            AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
    
            Dim i As Integer
    
            dt2.Columns.Add("index ", GetType(Integer))
            dt2.Columns.Add("description", GetType(String))
            dt2.Columns.Add("value1", GetType(Double))
            dt2.Columns.Add("value2", GetType(Double))
    
            dt2.Rows.Add(New Object() {1, "a", 5, 300})
            dt2.Rows.Add(New Object() {2, "b", 7, 400})
            dt2.Rows.Add(New Object() {3, "c", 8, 500})
            dt2.Rows.Add(New Object() {4, "d", 9, 600})
    
            ListBox1.Items.Clear()
    
            For i = 0 To dt2.Rows.Count - 1
                ListBox1.Items.Add(dt2.Rows(i).Item("description"))
            Next
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged
            TextBox1.Text = dt2.Rows.Item(ListBox1.SelectedIndex)("value1")
             TextBox2.Text = dt2.Rows.Item(ListBox1.SelectedIndex)("value2")
        End Sub
    
    End Class