Results 1 to 6 of 6

Thread: fill a combobox with a sorted files in dir c:\,ydir\

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,586

    fill a combobox with a sorted files in dir c:\,ydir\

    i have in a dir_

    1.txt
    2.txt
    55.txt
    20.txt
    9.txt
    11.txt
    ...
    6.txt
    123.txt

    how to fill a combobox with a sorted list of files, similar?

    1.txt
    2.txt
    6.txt
    9.txt
    11.txt
    20.txt
    55.txt
    123.txt

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: fill a combobox with a sorted files in dir c:\,ydir\

    list the files into an array using DIR to return all the files from the directory, sort the array using quicksort or bubblsort (depending on your preference), then add all the files to the combobox from the sorted array
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: fill a combobox with a sorted files in dir c:\,ydir\

    Suggestion...put the 'name' of the file (less ".txt") into a NUMERIC array Ex: Dim nArray() as Integer.
    An easy way to do that is to use a filelistbox set to the directory of your choice and then, using Instr()
    to find just the numeric portion of the filenames, put the filenames (less ".txt") into the array.

    Then simply use a Bubble Sort to output from lowest to highest.
    Finally, add the ".txt" string back.
    Sam I am (as well as Confused at times).

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: fill a combobox with a sorted files in dir c:\,ydir\

    Oh, I see westie has already provided an answer (using Dir instead of filelistbox). (But, WestConn, that array has to be a numeric one, yes?, as a 'normal' sort will sort by string...he will need just the 1, 2, 55, etc put into the array, and not the .txt extension.--correct?)
    Sam I am (as well as Confused at times).

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: fill a combobox with a sorted files in dir c:\,ydir\

    If you are not into arrays, here is an easy way to do what you want:

    (Add your combobox and a hidden, SORTED, listbox to your form)

    Code:
    Option Explicit
    Dim myFile As String
    Dim stringToAdd As String
    Private Sub Form_Load()
        Dim i As Integer, j As Integer
        myFile = Dir(App.Path & "\textfiles\*.txt")  'replace with YOUR directory of text files (this gets the first file)
        addZeros 'pad filename with preceeding zeros so list1 will be properly sorted
        List1.AddItem (stringToAdd & myFile)   
        Do While myFile <> ""  'add the remaining .txt files to the listbox
            myFile = Dir
            addZeros 'pad filenames
            List1.AddItem (stringToAdd & myFile)
        Loop
        removeZeros
    End Sub
    Private Sub addZeros()
            Select Case Len(myFile)
            Case 5
                stringToAdd = "0000"
            Case 6
                stringToAdd = "000"
            Case 7
                stringToAdd = "00"
            Case 8
                stringToAdd = "0"
            Case Else
                stringToAdd = ""
            End Select
    End Sub
    Private Sub removeZeros()  'when putting values in the combobox, remove the 'front' zeros
        Dim i As Integer, j As Integer
        For i = 1 To List1.ListCount - 1  'start at 1, not 0
            List1.ListIndex = i
            For j = 1 To Len(List1.Text) - 4
                If CInt(Mid(List1.Text, j, 1)) > 0 Then
                    Combo1.AddItem (Mid(List1.Text, j))
                    Exit For
                End If
            Next j
        Next i
        Combo1.ListIndex = 0
    End Sub
    Sam I am (as well as Confused at times).

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: fill a combobox with a sorted files in dir c:\,ydir\

    The Shell Lightweight API offers us a compare procedure for this.

    Very long lists don't belong in a list picker (ListBox) anyway, so fancy sorting should normally not be required:

    Code:
    Option Explicit
    
    Private Declare Function StrCmpLogicalW Lib "shlwapi" ( _
        ByVal psz1 As Long, _
        ByVal psz2 As Long) As Long
    
    Private Sub Form_Load()
        Dim FileName As String
        Dim pFileName As Long
        Dim I As Integer
    
        FileName = Dir$(App.Path & "\Files\*.*", vbNormal)
        With List1
            Do
                'If the list is very long you could do a fancier sort.
                If .ListCount = 0 Then
                    .AddItem FileName
                Else
                    pFileName = StrPtr(FileName)
                    For I = 0 To .ListCount - 1
                        If StrCmpLogicalW(StrPtr(.List(I)), pFileName) > -1 Then Exit For
                    Next
                    .AddItem FileName, I
                End If
                FileName = Dir$()
            Loop Until Len(FileName) = 0
        End With
    End Sub
    
    Private Sub Form_Resize()
        If WindowState <> vbMinimized Then
            List1.Move 0, 0, ScaleWidth, ScaleHeight
        End If
    End Sub
    Name:  sshot.png
Views: 106
Size:  1.5 KB
    Attached Files Attached Files

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