So my problem is that when I type for example "prod" in the SrchTBox and then click SrchBtn, it will only highlight "Product 2" but not "Product 1" even though they both have "prod" in their item name, how can I highlight any 2 or more relevant items? Also, how can I search items without the problem of worrying about case-sensitivity? That happened when I typed "prod" and it highlights the last item which is "Product 2" but not "Product 1." How can I also fix the bug where the program cannot read the text that I typed? (For example, I typed "rod" in the SrchTBox and a pop-up message shows up and it said, "No match has been found.")

NOTE: The problem is in SrchBtn.

Dim prdt As String() = New String(3) {}
Dim slctprdt As ListViewItem

Dim prdtName As String
Dim price As String
Dim qtty As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ProductListView.View = View.Details
    ProductListView.GridLines = True
    ProductListView.FullRowSelect = True

    ProductListView.Columns.Add("Product Name", 126)
    ProductListView.Columns.Add("Price", 93)
    ProductListView.Columns.Add("Quantity", 93)

    prdt(0) = "Product 1"
    prdt(1) = "100"
    prdt(2) = "10"
    slctprdt = New ListViewItem(prdt)
    ProductListView.Items.Add(slctprdt)

    prdt(0) = "Product 2"
    prdt(1) = "200"
    prdt(2) = "20"
    slctprdt = New ListViewItem(prdt)
    ProductListView.Items.Add(slctprdt)

End Sub
Private Sub SlctBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    If ProductListView.SelectedItems.Count = 0 Then
        MsgBox("ERROR: No items selected")
    Else
        prdtName = ProductListView.SelectedItems.Item(0).SubItems(0).Text
        price = ProductListView.SelectedItems.Item(0).SubItems(1).Text
        qtty = ProductListView.SelectedItems.Item(0).SubItems(2).Text

        MsgBox(prdtName & " " + price & " " & qtty)
    End If

End Sub
Private Sub AddPrdtBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Dim prdt As String() = New String(3) {}
    Dim slctprdt As ListViewItem

    If PrNTBox.Text <> Trim("") And PTBox.Text <> Trim("") And QTBox.Text <> Trim("") Then
        prdt(0) = PrNTBox.Text
        prdt(1) = PTBox.Text
        prdt(2) = QTBox.Text
        slctprdt = New ListViewItem(prdt)
        ProductListView.Items.Add(slctprdt)
        PrNTBox.Clear()
        PTBox.Clear()
        QTBox.Clear()
    Else
        MsgBox("ERROR: Incomplete textboxes")
    End If

End Sub

Private Sub SrchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SrchBtn.Click

    Dim srchTxt = Me.SrchTBox.Text

    If srchTxt = String.Empty Then
        MsgBox("Please enter the search box first.")
    Else
        Dim strtIndx = 0
        Dim prdt As ListViewItem = Nothing

        If Me.ProductListView.SelectedItems.Count = 1 AndAlso Me.ProductListView.SelectedItems(0).Text = srchTxt Then
            strtIndx = Me.ProductListView.SelectedIndices(0) + 1
        End If

        If strtIndx < Me.ProductListView.Items.Count Then
            Do
                prdt = Me.ProductListView.FindItemWithText(srchTxt, False, strtIndx)

                If prdt Is Nothing OrElse prdt.Text = srchTxt Then
                    Exit Do
                End If

                strtIndx = prdt.Index + 1

                If strtIndx >= Me.ProductListView.Items.Count Then
                    Exit Do
                End If
            Loop
        End If

        Me.ProductListView.SelectedItems.Clear()

        If prdt Is Nothing Then
            MsgBox("No match has been found.")
        Else
            prdt.Selected = True
            prdt.EnsureVisible()
            Me.ProductListView.Select()
        End If
    End If
End Sub

Recommended Answers

With a specific reference to the search part of this question and where we read line 79 consider:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listview.finditemwithtext?view=net-5.0

FindItemWithText(String)
Finds the first ListViewItem that begins with the specified text value

Jump to Post

All 2 Replies

Via an e-mail alert you wrote:

So this also works for numbers as well?

The items in this control are text so the number is text here so yes to that question via email.

Be a part of the DaniWeb community

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