Solved

Search a ListView using Selected ComboBox Value

Posted on 2009-07-09
5
981 Views
Last Modified: 2012-05-07
I need to search a listview, highlight the found value and display the result in textboxes using the selected value from a ComboBox.
0
Comment
Question by:wsenter
5 Comments
 
LVL 2

Expert Comment

by:pr_r
ID: 24820334
Can u be bit eloborate.
if you have tried it can u paste the code also.
0
 
LVL 1

Author Comment

by:wsenter
ID: 24820348
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.

Thanks
0
 
LVL 2

Expert Comment

by:lppperes
ID: 24820364
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
            ComboBox1.Items.Add(index.ToString())
            ListBox1.Items.Add(index.ToString())
        Next
    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
0
 
LVL 15

Accepted Solution

by:
angus_young_acdc earned 500 total points
ID: 24821265
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

0
 
LVL 1

Author Closing Comment

by:wsenter
ID: 31601941
Excellent Bud!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question