CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Sorting a Listview acting as treeview (topmost parent only)

    I have initially thought of that method but that will work if the items are already sorted either ascendingly or descendingly but will not be 100% accurate if the items are not yet sorted since the first two items can be sorted and yet the third and succeeding ones can be unsorted.

  2. #17
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Sorting a Listview acting as treeview (topmost parent only)

    Not if sorted is turned on. It will be sorted in one direction or the other
    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!

  3. #18
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Sorting a Listview acting as treeview (topmost parent only)

    Got it sorted out. Here it is in its glory.
    Attached Files Attached Files

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

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    Well done.
    Your ArrayOrder() function could be fooled by an initial random order where incidentally the first 3 elements are in ascending order.
    But never mind. It works very good as it is.
    Also the quickort is much more sophisticated than my little bubbler.

    I remember, there was a ListView hack with usage of SendMessage() which put little icons of up-arrow or downarrow into the columnheader, thus signalling if the column was sorted ascending or descending. I shall try to find this again. Could be helpful.

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

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    Ok I found this:
    Maybe it is worth a look. By adding the up-arrow or down-arrow to a column header you have got a sure flag which order you got. So you can determine easily which order you have to sort when clicking the header.
    Could be a nice extension to your little program.

    http://www.buygold.net/v05n07/v05n07.html

  6. #21
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    Quote Originally Posted by WoF View Post
    Your ArrayOrder() function could be fooled by an initial random order where incidentally the first 3 elements are in ascending order.
    Hmmmnnn... Could you demonstrate such scenario? I tried with the sample data below and it worked still.

    Code:
    Private Sub Command1_Click()
        Dim data(5) As String
        
        data(0) = "Item11"
        data(1) = "Item12"
        data(2) = "Item13"
        data(3) = "Item1"
        data(4) = "Item3"
        data(5) = "Item2"
        
        MsgBox ArrayOrder(data)
        
    End Sub
    Public Function ArrayOrder(pvarArray As Variant) As Long
        Dim lngDirection As Long
        Dim i As Long
        
        If Not IsArray(pvarArray) Then Exit Function
        For i = LBound(pvarArray) To UBound(pvarArray) - 1
            Select Case lngDirection
                Case 1: If pvarArray(i) > pvarArray(i + 1) Then Exit Function
                Case -1: If pvarArray(i) < pvarArray(i + 1) Then Exit Function
                Case Else
                    If pvarArray(i) < pvarArray(i + 1) Then
                        lngDirection = 1
                    ElseIf pvarArray(i) > pvarArray(i + 1) Then
                        lngDirection = -1
                    End If
            End Select
        Next
        ArrayOrder = lngDirection
    End Function

  7. #22
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    No icons, but I have two option butons:
    Attached Files Attached Files
    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!

  8. #23
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    Quote Originally Posted by WoF View Post
    I remember, there was a ListView hack with usage of SendMessage() which put little icons of up-arrow or downarrow into the columnheader, thus signalling if the column was sorted ascending or descending. I shall try to find this again. Could be helpful.
    Thank you for this one but then again, what if new unsorted rows are added to the listview? In effect such will break the symbol.

  9. #24
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    Not if they're added in the proper location (automagically)
    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!

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

    Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)

    @David: Right, but the sample uses an unsorted LV because of the unorthodox tree structure it can take. So no automagically sorting in of new items.

    @Dee-U, you are right about the flag thing. Maybe you better update the arrow symbols according the actual sort order.

    What concerns your ArrayOrder() function.
    I thought it was supposed to return 0 for an unsorted array, -1 for a descending and +1 for an ascending order.
    But it returns 0 for an unsorted AND for an ascending array.
    Try
    Code:
        data(0) = "Item1"
        data(1) = "Item2"
        data(2) = "Item3"
        data(3) = "Item4"
    I thought this should produce 1, signalling an ascending order, but it comes out 0.
    It produces the -1 for descending arrays, though.
    And I have to apology: it is NOT fooled by a random sequence as I thought in the first place.
    But it is fooled by a properly sorted array.
    Last edited by WoF; March 11th, 2009 at 09:32 AM.

Page 2 of 2 FirstFirst 12

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