Author Topic: dir listbox  (Read 1859 times)

0 Members and 1 Guest are viewing this topic.

Humbertogo

  • Guest
dir listbox
« on: January 16, 2008, 02:49:37 PM »
is there any possibility to use a dir listbox in VBA

Guest

  • Guest
Re: dir listbox
« Reply #1 on: January 16, 2008, 03:01:36 PM »
Unfortunately, no.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: dir listbox
« Reply #2 on: January 16, 2008, 03:03:31 PM »
list of directories or list of files?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: dir listbox
« Reply #3 on: January 16, 2008, 03:18:42 PM »
I found this HERE
Quote
To begin you will pull each directory value into the array. Next you will need to sort the array to present the information in a clear manner. Last you will place the directory information into the list box control.

The routine below requires two parameters, the list box control and a string containing the path to the directory you need to display.

To pull all the values in the directory you pull the first file name with

    file_name = Dir$(strDrive & "\*.*", vbDirectory)

The parameter vbDirectory indicates that you are looking at a directory. The value is placed in the array and then each subsequent value is requested using

    file_name = Dir$()

When all values have been placed in the array this routine calls a quick sort routine to sort the values. Last, the sorted listi s placed in the list box control by setting its row source property to the values from the sorted array.


Public Sub FillList(cmbList As ListBox, strDrive As String)

Const ArrTop As Integer = 300 ' limit to 300 files, and that is way too many
' list box row source property is limited to
' 2048 characters so at 300 files that is
' about 7 characters per name
Dim MyArray(ArrTop) As String

Dim file_name As String
Dim strRowSource As String ' Build row source string here to get all files
Dim i As Integer ' MyArray index for fill
Dim j As Integer ' MyArray index for move to list box

On Error Resume Next
'list the names of all files in the specified directory

file_name = Dir$(strDrive & "\*.*", vbDirectory)
i = 1
Do While (Len(file_name) > 0) And (i < 101)

    ' See if we should skip this file.
    If Not (file_name = ".") Or (file_name = "..") Then
        MyArray(i) = file_name
        i = i + 1
    End If

    ' Get the next file.
    file_name = Dir$()
Loop

If i > 1 Then

    ' note here is the code for QuickSort subroutine
    QuickSort MyArray, 1, i - 1
    'use sorted array to refill listbox
    cmbList.RowSource = "" 'clears the list
    For j = 1 To i
        strRowSource = strRowSource & MyArray(j) & ";"
    Next j

        ' If there are too many files take the most recent
        'RowSource string length is 2048 characters
        cmbList.RowSource = Left(strRowSource, 2048
    Else
    cmbList.RowSource = MyArray(1)
End If

End Sub
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4043
Re: dir listbox
« Reply #4 on: January 16, 2008, 03:20:46 PM »
And another example HERE
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second

Humbertogo

  • Guest
Re: dir listbox
« Reply #5 on: January 18, 2008, 03:37:45 AM »
I can not set the RowSource Properties
get err Invalid property value

Autodesk Architectural Desktop 2007 , Microsoft Office 2007 ,Win XP Pro sp2