Results 1 to 6 of 6

Thread: Vb6 - Sort Listview By Dates/numbers/text

  1. #1
    Moderator.NET kleinma's Avatar
    Join Date
    Nov 01
    Location
    NJ - USA (Near NYC)
    Posts
    23,021

    Vb6 - Sort Listview By Dates/numbers/text

    the title explains it all

    as most of you know, you can only do text sorts on listview in report style, which is a big pain. MS provides an API method in the KB on how to sort by dates, but it is cumbersome, and it doesn't order the listitems, so after you use it, you can't do a for each listitem in listitems loop because the order of the listitems collection doesn't change. this code fixes all that, and doesn't even use any API

    there is an example attached that will show you how it works.
    Attached Files Attached Files
    Using VB.NET 2010/.NET 2.0 through 4.0 * Please mark you thread resolved using the Thread Tools above
    PLEASE INDICATE WHAT VERSION OF VB YOU USE!!!!!!!!!!!
    * If you found a post useful then please Rate it! * DO NOT PM ME WITH LINKS TO YOUR THREADS FOR ANSWERS PLEASE!

    Code Bank:Manipulate HTML Page content in the Web Browser Control from VB - Drag Drop from Windows into Win Form - Launch new default browser instance to open URL - Display Internet Image in Picturebox - Download Files From Web With Progress Bar - IP Textbox User Control - Installing .NET Framework with INNO Setup
    ZerosAndTheOne.com
    -=Matt=-

  2. #2
    Hyperactive Member
    Join Date
    Aug 08
    Posts
    322

    Re: Vb6 - Sort Listview By Dates/numbers/text

    here is another simple example :

    Code:
    Private Sub ListView2_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    
        ListView2.SortKey = ColumnHeader.Index - 1
    
        If ListView2.SortKey = 1 Then ListView2.SortKey = 3 ' **** This column changes the key
        ListView2.SortOrder = (ListView2.SortOrder - 1) * -1
        ListView2.Sorted = True
    
    End Sub
    Thanks for helping me out.

  3. #3
    Fanatic Member
    Join Date
    May 09
    Posts
    868

    Re: Vb6 - Sort Listview By Dates/numbers/text

    Hi, Fantastic code as it's doing what i want to do kind of. Any idea how I can make it sort numbers correctly.

    For example i have

    1000
    2000
    9,900
    22,000
    82,000


    and when I sort it it only sorts by the first number. I was hoping it would sort by highest amount as apose to the first number?

    I basically want to sort it via highest value / lowest value.


    any ideas?


    Jamie.

  4. #4
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Vb6 - Sort Listview By Dates/numbers/text

    One way is to make all the numbers have the same length by
    prepending spaces. Formatting with the '@' character
    right justifies the number with spaces.

    Code:
    Private Sub Form_Load()
     Dim n As String
     Dim i As Long
     'change this constant, depending on expected number length
     ' or calculate it
     Const MaxLen As Long = 6
     For i = 1 To 10
      n = Format$(i, String$(MaxLen, "@"))
      Debug.Print n, Len(n)
     Next
    End Sub

  5. #5
    Fanatic Member
    Join Date
    May 09
    Posts
    868

    Re: Vb6 - Sort Listview By Dates/numbers/text

    Hi mate, I'm really confused as to where I should put that code.

    Basically at the moment in the listview when I sort it, it will go by the first number not the whole figure...

    Any ideas?

  6. #6
    Fanatic Member
    Join Date
    Mar 09
    Posts
    721

    Re: Vb6 - Sort Listview By Dates/numbers/text

    What that code shows is making the numbers the same length
    with prepended spaces so the numbers will sort properly.

    The code below shows using numbers in the 2nd column (SubItem(1))

    Code:
    Option Explicit
    Private Sub Form_Load()
     Dim i As Long
     Dim Num As String
     Const MaxLen As Long = 8 'change as necessary for the
                              'expected length of the numbers
     Randomize
     ListView1.Sorted = False
     For i = 1 To 100
      With ListView1.ListItems.Add
       .Text = "Item " & i
       Num = Format$(Int(Rnd * i * 1000) + 1, String$(MaxLen, "@"))
       'you can see how this works by using '0' vice '@' in the above line
       .SubItems(1) = Num
      End With
     Next
     ListView1.SortKey = 1 'subitem(1)
     ListView1.Sorted = True
    End Sub
    
    Private Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
     With ListView1
      .SortKey = ColumnHeader.Index - 1
      .SortOrder = .SortOrder Xor 1 'toggle ascending/descending
     End With
    End Sub

Posting Permissions

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