Good day to all.

I'm a VB.NET newbie.


How can I search, for example ID Number,
and want to highlight the listview item found in search
from my first form?

I'm using combobox from my second form to search.

Thanks.

Recommended Answers

All 6 Replies

Hi,

How is the information your searching stored? for examle is it stored in a database.

Cheers

John

See if this helps.
1 ListView, 1 TextBox(for search value), 1 Button(to search)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not ListView1.MultiSelect = False Then ListView1.MultiSelect = False '// disable MultiSelection of items.
        If TextBox1.Text = "" Then Exit Sub '// do not search on empty search value.
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items.
            If itm.Text = TextBox1.Text Then '// check if itm.Text equals search value.
                itm.Selected = True '// select item.
                itm.EnsureVisible() '// if itm is not visible in ListView, make sure it is.
                ListView1.Select() '// select the ListView to show highlighted item.
                Exit For '// exit loop since itm found.
            End If
        Next
    End Sub

Or you can change the item's.BackColor if found.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then Exit Sub '// do not search on empty search value.
        For Each itm As ListViewItem In ListView1.Items '// loop thru all items.
            If itm.Text = TextBox1.Text Then '// check if itm.Text equals search value.
                itm.BackColor = Color.GreenYellow   '// change item.BackColor if found.
                itm.EnsureVisible() '// if itm is not visible in ListView, make sure it is.
            Else
                itm.BackColor = ListView1.BackColor '// change all other items BackColor to ListView.BackColor.
            End If
        Next
    End Sub

Sir, i get the NullReference exception. I was using VBFixedString for my listview.

Sir I get NullReference Exception from the following:

(Clientform.Clienttable is my Listview from my first form, ClientForm.
I put the search code on the second form, InputForm. I have 1 textbox and a button, sir.)

Try
            If Not ClientForm.ClientTable.MultiSelect = False Then
            ClientForm.ClientTable.MultiSelect = False

            If searchbox.Text = "" Then Exit Sub
            For Each itm As ListViewItem In ClientForm.ClientTable.Items
                If itm.Text = searchbox.Text Then
                    itm.Selected = True
                    findlrecords()  '<--- the yellow highlighted line
                    itm.EnsureVisible()
                    ClientForm.ClientTable.Select()
                    Exit For
                End If
            Next
        Catch
            MsgBox(about, MsgBoxStyle.Exclamation)
        End Try
    End Sub

and sir, I have my VBFixed String and other details here:


For VBFixed String (Declared in my module):

Structure clients
        <VBFixedString(32)> Dim s1 As String
        <VBFixedString(32)> Dim s2 As String
        <VBFixedString(32)> Dim s3 As String
        <VBFixedString(32)> Dim s4 As String
        <VBFixedString(32)> Dim s5 As String
        <VBFixedString(32)> Dim s6 As String
        <VBFixedString(32)> Dim s7 As String
        <VBFixedString(32)> Dim s8 As String
        <VBFixedString(32)> Dim s9 As String
        <VBFixedString(32)> Dim s10 As String
        <VBFixedString(32)> Dim s11 As String
        <VBFixedString(32)> Dim s12 As String
        <VBFixedString(32)> Dim s13 As String
    End Structure

and my findrecords() (on my module too.):

Public Function findlrecords() As Integer
        Dim temp As clients, fn As Integer
        fn = FreeFile()
        FileOpen(fn, filename, OpenMode.Random, OpenAccess.Read, , Len(temp)) <--also the highlighted line.
        findlrecords = 1
        Do While Not EOF(fn)
            FileGet(fnum, temp)
            findlrecords = findlrecords + 1
        Loop
        FileClose(fnum)
    End Function

I added findlrecords() to search my listview using System.io (I use system.io namespace in module).

Seems like this new issue is not caused by the code I have provided, but by code in your Public Function findlrecords() As Integer .
You can add a Try/Catch statement in your Function for the time being and start a new thread for this new issue caused by your Function.

Glad I could help otherwise.

I will try to redefine and recheck my code sir,
and post a new thread if new errors were found as possible.

By the way, thank you for the help in Search, sir.

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.