i have a combobox that contains the items 0001,0002,0003, etc. i have a listview with 3 columns namely ID, Name , Age. I want to do is when i select the ID "0001" in the combobox, it will display the name and the age in the textbox1 and textbox2 that is from the listview . Thank you

Recommended Answers

All 5 Replies

Try something like this:

For Each lviItem As ListViewItem In ListView1.Items
    If lviItem.Text = ComboBox1.Text Then
        TextBox1.Text = lviItem.SubItems(0).Text
        TextBox2.Text = lviItem.SubItems(1).Text
    End If
Next

If you set the Name property of each item when you add the items to the ListView then you don't have to do a search. You can access by key as in

Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Dim key As String = DirectCast(sender, ComboBox).Text

    If ListView2.Items.ContainsKey(key) Then
        txtName.Text = ListView2.Items(key).SubItems(1).Text
        txtAge.Text = ListView2.Items(key).SubItems(2).Text
    End If

End Sub

An example of adding with keys is

item = New ListViewItem({"0001", "George", "42"})
item.Name = item.SubItems(0).Text
ListView2.Items.Add(item)
For Each lviItem As ListViewItem In ListView1.Items
    If lviItem.Text = ComboBox1.Text Then
        TextBox1.Text = lviItem.SubItems(0).Text
        TextBox2.Text = lviItem.SubItems(1).Text
    End If

wher i put this code sir? i'll try to put it in the combobx Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

but it is not working . thanks

but it is not working

Be specific. In what way is it not working?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.