Search a ListView using Selected ComboBox Value

I need to search a listview, highlight the found value and display the result in textboxes using the selected value from a ComboBox.
Who is Participating?
angus_young_acdcConnect With a Mentor Commented:
The following C# code will do what you're looking for based upon whatever combobox ID you have selected:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            // Loop through the list view checking the first subitem (which is your first column
            foreach (ListViewItem item in listView1.Items)
                if (item.SubItems[0].Text == comboBox1.Text)
                    item.Selected = true;
                    textBox1.Text = item.SubItems[0].Text; // your customer id
                    textBox2.Text = item.SubItems[1].Text; // Your customer name

Open in new window

Can u be bit eloborate.
if you have tried it can u paste the code also.
wsenterAuthor Commented:
I have a form with a listview control that is populated with CustomerID, CustomerName. I have a combo box on the form that is filled with the same information.  I am using suggest/append setting on the combo box for the user to search for the customer. When they find the customer, they press tab for example. Then, take the CustomerID from the combo box and lookup the matching entry in the listview. highlight the listview selection, lookup the record and display in text fields.

You can try something like this:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For index As Integer = 1 To 10
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim text As String = ComboBox1.SelectedItem.ToString()
        Dim index As Integer = ListBox1.FindStringExact(text)
        If index <> 0 Then
            ListBox1.SelectedIndex = index
            TextBox1.Text = ComboBox1.SelectedItem.ToString()
        End If
    End Sub
wsenterAuthor Commented:
Excellent Bud!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.