sort a field in listview items by letter by letter entered in textbox txtsearch
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: sort a field in listview items by letter by letter entered in textbox txtsearch

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    sort a field in listview items by letter by letter entered in textbox txtsearch

    Hai Every one I am new to this forum
    I wrote code to sort listview items letter by letter ie., a,b,s like that (indexing the column)
    The code I wrote is :
    Code:
    Private Sub txtsearch_Change()
    ListView1.ListItems.Clear
    Dim list As ListItem
    Dim x As Integer
    connectDB
    rs.Open "Select*from accreg where Title line'"&txtsearch&"%'", db, 3, 3
    Do Until rs.EOF
    Set list = ListView1.ListItems.add(, , rs(0))
    For x = 1 To 3 'number of fields in the date base minus one
    list.SubItems(x) = rs(x)
    Next x
    rs.MoveNext
    Loop
    Set rs = Nothing
    db.Close: Set db = Nothing
    End Sub
    But the error is Compile error

    syntax error the following line of code coloured in Red.
    Code:
    rs.Open "Select*from accreg where Title line'"&txtsearch&"%'", db, 3, 3
    kindly see the code above and give your valuable suggestion.http://www.youtube.com/results?searc...+Part+6&page=4 i need the result of code in video i.e, search fullname

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

    Re: sort a field in listview items by letter by letter entered in textbox txtsearch

    Change to
    Code:
    rs.Open "Select * from accreg where Title like '" & txtsearch.text & "%'", db, 3, 3
    I'm not quite sure, but I think there are some spaces required, at least between "like" and the searchtext, also after Select, I think.
    Try this line and tell us if it works any better.

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Re: sort a field in listview items by letter by letter entered in textbox txtsearch

    Mr.WoF Sir,

    I am (very)n (n is positive integer) Thankful to you for your reply which is perfectly working. I am working on this bit of code ie., txtsearch_change from last 15 days.

    Thanks once again

    I wish to proceed further in my project until then may leave this thread intact or should be closed, if necessary to close, let me know how to close a thread.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,986

    Re: sort a field in listview items by letter by letter entered in textbox txtsearch

    Thread Tools, at the TOP of this post...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2012
    Posts
    7

    Re: sort a field in listview items by letter by letter entered in textbox txtsearch

    To do this you need win32 api, it's not hard, not easy:
    Your sort function contains something like this (sample is for date sorting, but you can change it for other datatypes):

    On sort:
    SendMessage Listview.hWnd, LVM_SORTITEMS, Listview.hWnd, ByVal lp2val(AddressOf ListviewCompareDate)

    Function ListviewCompareDate&(ByVal lParam1&, ByVal lParam2&, ByVal hWnd&)

    'Param1 and Param2 are the values to compare
    'mbSorted contains the sort state of the Listview

    Dim d1 As Date, d2 As Date
    d1 = ListviewItemDate(hWnd, lParam1)
    d2 = ListviewItemDate(hWnd, lParam2)

    If d1 = d2 Then
    ListviewCompareDate= 1
    Else
    ListviewCompareDate= IIf(mbSorted Xor d1 > d2, 0, 2)
    End If

    End End

    Private Function ListviewItemDate(hWnd&, lParam&) As Date

    Dim hX&, uFI As LV_FINDINFO, uLi As LV_ITEM, l&

    uFI.flags = LVIF_TEXT
    uFI.lParam = lParam
    hX = SendMessage(hWnd, LVM_FINDITEM, -1, uFI)

    With uLi
    .mask = LVIF_TEXT
    .iSubItem = miSortKey
    .pszText = Space$(32)
    .cchTextMax = Len(.pszText)
    l = SendMessage(hWnd, LVM_GETITEMTEXT, hX, uLi)

    If l Then ListviewItemDate = CDate(Left$(.pszText, l))

    End With

    End Function

    You can create your own function to change datacompare for Integers, strings or whatever you like.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center