CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2003
    Posts
    38

    Can ListBox.Sorted Property be changed?

    Can I set the sorted property true/false in code or is it only available in properties?

    I've tried ListBox.Sorted = True and it says it's a read only property.

  2. #2
    Join Date
    Apr 2002
    Location
    Los Angeles, Ca
    Posts
    238
    Originally posted by winke04

    I've tried ListBox.Sorted = True and it says it's a read only property.
    Read-only is READ ONLY ... you cannot write to or manipulate a read only property.

  3. #3
    Join Date
    Nov 2003
    Location
    Seattle, WA
    Posts
    265
    But if you really want it sorted you can sort it using something like the Bubble Sort algorithm, I have only used it with Numerics, but I'm sure you can do it with strings too. You would just have to modify it to sort strings.

    Code:
    Public Sub BubbleSort(ByRef nSort() As Integer, Optional stAccending As Boolean = True)
        Dim bFound As Boolean
        Dim t As Integer
        Dim j As Integer
        'Dim passes As Long
        'Dim replacments As Long
        
        bFound = True
            
        Do Until bFound = False
            bFound = False
            'passes = passes + 1
            For j = UBound(nSort) - 1 To LBound(nSort) Step -1
                Select Case stAccending
                    Case True
                        If nSort(j + 1) < nSort(j) Then
                            DoEvents
                            bFound = True
                            t = nSort(j)
                            nSort(j) = nSort(j + 1)
                            nSort(j + 1) = t
                            'replacments = replacments + 1
                        End If
                    Case False
                        If nSort(j + 1) > nSort(j) Then
                            DoEvents
                            bFound = True
                            t = nSort(j)
                            nSort(j) = nSort(j + 1)
                            nSort(j + 1) = t
                            'replacments = replacments + 1
                        End If
                End Select
            Next
        Loop
    End Sub

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332
    QuickSort will be faster. However, you can just set it to be sorted at design time, then use an index to add items when you don't want it sorted. For instance, the following will always add to the bottom of the list, even when it is sorted:
    List1.AddItem "New Line", List1.ListCount
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Nov 2003
    Location
    Seattle, WA
    Posts
    265
    Hmm.. I never heard of quick sort, I've only heard of Bubble Sort, Insertion Sort, Selection Sort, and two others I can't remember right now.. If quick sort has something to do with creating some kind of partition of an array then maybe I have seen it before.

    I will have to look into that quick sort, will make all my huge sorts faster

  6. #6
    Join Date
    Dec 2003
    Posts
    38
    I copped out for now by adding another listbox. One is sorted and one is not.

    I have a large Access DB to keep track of all my MP3 files. I have tables of Artists, Albums, and Songs. When I list all songs I want them in alpha order. When I list the songs on a selected album I want them in the order they are on the album which is the order they are stored in the DB. If I could have just set the Sorted property at run time I could have used the same listbox. As it turned out there weren't too many places I had to do an If Then Else to use either listbox.

    I'll sure keep the sort info in mind though. I know there will be uses for it coming up.

    Now if I can just figure out this packaging and deployment. What a pain that is.

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332
    Well, then you should be able to leave it sorted, and just specify the index as stated above, and it will be as you want.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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