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.

Dani AI

Generated

Short summary: is searching a ListView on a different form and getting a NullReferenceException after trying the loop-based suggestions from . Two common culprits show up in this thread: (1) the referenced form or its ListView is not the same instantiated object that actually contains the data, and (2) the Random-file routine (findlrecords) has inconsistent variable usage and/or an undefined filename which will break file I/O.

A safer, clearer pattern is to let the form that owns the ListView do the selection. Expose a small public method on ClientForm that performs the lookup and selection; the search form simply calls that method. This avoids cross-form control access and simplifies threading/visibility concerns.

Example (on the main form that owns the ListView):

Public Sub SelectClientByID(id As String)
    If String.IsNullOrEmpty(id) OrElse ClientTable Is Nothing Then Return
    Dim found As ListViewItem = ClientTable.FindItemWithText(id, True, 0, True)
    If found IsNot Nothing Then
        found.Selected = True
        found.EnsureVisible()
        Me.BringToFront()
        Me.Activate()
    End If
End Sub

Example call from the search form:

If Not String.IsNullOrEmpty(searchbox.Text) AndAlso ClientForm IsNot Nothing Then
    ClientForm.SelectClientByID(searchbox.Text)
End If

Additional troubleshooting notes: the posted findlrecords() shows a likely typo (fn declared, then fnum used in FileGet/FileClose). Consistent use of the same file handle variable and ensuring the filename is defined are necessary for correct Random file access. Also consider replacing legacy VBFixedString/random-file storage with a List(Of T) or simple local DB for easier maintenance. These changes address the NullReferenceException and make the search flow more robust and maintainable.

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.