Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Searching listview by filtering by codes

joshface04

New Coder
I have some code which allows us to perform searches/filter data on a list view control. By typing some search criteria into a textbox, the code goes through every listview item and if it matches it keeps it, if not, it gets rid of it. The code itself works great but we also want to be able to use radio buttons to search for products listed in a listview column.

The code checks which radio button is pressed and uses the to determine the product. The code then checks the serial number column in the listview control and if a serial njumber contains two specific letters which are a code for a product, it keeps that listviewitem.

Here's my code:

Code:
   Dim Product As String
            If rbtn_Empower.Checked = True Then
                Product = "EM"
            End If
            Dim pos2 As Int32
            Dim listItem2 As ListViewItem
            For pos = SUPID_Data.ListView_Search.Items.Count - 1 To 0 Step -1
                listItem2 = SUPID_Data.ListView_Search.Items(pos)
                If Not listItem2.SubItems(2).Text.IndexOf(Product, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                    SUPID_Data.ListView_Search.Items.Remove(listItem2)
                End If
            Next

However; we have several codes for products, so instead of just 'EM' in this example, we need 'EM' 'ML' and 'MX'. I've tried the variable 'product' as an array and put the codes in there but had no joy. What can I do?
 
Code:
        If rbtn_Empower.Checked = True Then
            Dim Product() As String = {"EM", "ML", "MX"}
            Dim pos2 As Int32
            Dim listItem2 As ListViewItem
            Dim i as Int32
            For pos = SUPID_Data.ListView_Search.Items.Count - 1 To 0 Step -1
                listItem2 = SUPID_Data.ListView_Search.Items(pos)
                For i = 0 To Product.Length
                    If listItem2.SubItems(2).Text.IndexOf(Product[i], 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                        Exit For
                    End If
                Next
                If i == Product.Length Then
                    SUPID_Data.ListView_Search.Items.Remove(listItem2)
                End If
            Next
        End If
 
Last edited:
Code:
        If rbtn_Empower.Checked = True Then
            Dim Product() As String = {"EM", "ML", "MX"}
            Dim pos2 As Int32
            Dim listItem2 As ListViewItem
            Dim i as Int32
            For pos = SUPID_Data.ListView_Search.Items.Count - 1 To 0 Step -1
                listItem2 = SUPID_Data.ListView_Search.Items(pos)
                For i = 0 To Product.Length
                    If listItem2.SubItems(2).Text.IndexOf(Product[i], 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                        Exit For
                    End If
                Next
                If i == Product.Length Then
                    SUPID_Data.ListView_Search.Items.Remove(listItem2)
                End If
            Next
        End If
I'm getting an error with the Product bit and Visual Studio says there's an overload resolution error because 'IndexOf' accepts a certain amount of arguments, I changed it to Product(i) which got rid of the error. Also "If i == product.length Then" should only have one equals sign, C# accepts two equals signs for equal to, VB.NET uses one. However; after changing the code the program starts now because there's no errors but when I do a search and use the Empower radio button to select that type of search I get 'System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'' error on the following line:

Code:
          If listItem2.SubItems(2).Text.IndexOf(Product(i), 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
 
I found a bug in the code. Just fixed it. Try:

Code:
        If rbtn_Empower.Checked = True Then
            Dim Product() As String = {"EM", "ML", "MX"}
            Dim pos2 As Int32
            Dim listItem2 As ListViewItem
            Dim i as Int32
            Dim found as Boolean
            For pos = SUPID_Data.ListView_Search.Items.Count - 1 To 0 Step -1
                listItem2 = SUPID_Data.ListView_Search.Items(pos)
                found = False
                For i = 0 To Product.Length  - 1
                    If listItem2.SubItems(2).Text.IndexOf(Product[i], 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then
                        found = True
                        Exit For
                    End If
                Next
                If not found Then
                    SUPID_Data.ListView_Search.Items.Remove(listItem2)
                End If
            Next
        End If
 

Buy us a coffee!

Back
Top Bottom