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