CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    62

    Unhappy how to search text in Listview

    I am displaying records like p.o.no, item no etc. in Listview. I want write code for searching perticular string may be item no or p.o.no. for that i ve written following code

    For ivar = 1 To ListView1.ListItems.Count
    If ListView1.ListItems(ivar).ListSubItems(4).Text = txtsearch.Text Then

    ListView1.ListItems(ivar).Selected = True
    End If
    Next

    in txtsearch.Text i m entering text which i want to search.is this code correct?

    above code is not working ,it is not selecting the complete row which having the entered text.

    please give me solution.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: how to search text in Listview

    Hello,

    at first glimpse it looks as if you are only searching text in the SubItems(4) column. If you want to search in all columns and in the main item you have to nest an inner loop going through all existing subitmems. Also it may be save to make the search insensitive to case and compare not for exact equality but rather if the search string is part of a fields text.

    Try:
    Code:
    For ivar = 1 To ListView1.ListItems.Count
          If instr(LCase(ListView1.ListItems(ivar).text), LCase(txtsearch.text)) then
             ListView1.ListItems(ivar).Selected = True 'found match
          else 'search the subitems
              For si=1 to ListView1.Listitems(ivar).SubItems.Count
                    If instr(LCase(ListView1.ListItems(ivar).SubItems(si).text), LCase(txtsearch.text)) then
                       ListView1.ListItems(ivar).Selected = True 'found match in a subitem
                    End If
              Next si
          End If
    Next ivar
    Last edited by WoF; July 11th, 2006 at 08:20 AM. Reason: Mark Code section

  3. #3
    Join Date
    Oct 2005
    Posts
    62

    Re: how to search text in Listview

    hi wof,
    thanx for helping me, i have tried ur code but it's giving me error at subitem " invalid qualifier"

    For ivar = 1 To ListView1.ListItems.Count
    If InStr(LCase(ListView1.ListItems(ivar).SubItems(4).Text), LCase(txtsearch.Text)) Then

    ListView1.ListItems(ivar).Selected = True
    ivar2 = ivar
    End If
    Next
    End If
    i have mention 4 because the p.o. no is 4 the column .so that if i select p.o. no in text box then it will search that text in 4th column & not all column. same for item no, i will search that in 3rd column.
    pls give me solution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured