Link to home
Start Free TrialLog in
Avatar of Shroom
Shroom

asked on

How do you add a dir toa list box? (urgent)

How do you add like all the names of the directories in a directory to a combo box? Then add the files of a directory to a list box? Please give a vivid discription I am slow :-) and lazy. :-( YaY thanx.
Avatar of Ark
Ark
Flag of Russian Federation image

Avatar of AshokKumar
AshokKumar

Comeon friend, y u need this. U have a built in control shipped with VB namely File list box and directory list box. ANyway, if u need this, just try this. Place a directory list box and a sile list box in the form. Hide both controls. Give the Initial path for directory list box as the directory u wish and the file list box path property as directory list box's path. Then all the sub directories will be loaded in the directory list box and files in the corresponding folder into file list box. Nowe just add the items in the combo box and list box respectively. Simple. Right? Pls. get baqck to me.
ASKER CERTIFIED SOLUTION
Avatar of mcrider
mcrider

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
by the way, the code I gave you does not require a DirListBox or a FileListBox...

Also to change the directory you want to load, change the "lPath" variable...


Cheers!®©
Hi
If you too lazy, get all the code
You need form with List1 listbox on it

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const LB_DIR = &H18D
Const LB_RESETCONTENT = &H184

Const DDL_READWRITE = &H0
Const DDL_READONLY = &H1
Const DDL_HIDDEN = &H2
Const DDL_SYSTEM = &H4
Const DDL_DIRECTORY = &H10
Const DDL_ARCHIVE = &H20
Const DDL_DRIVES = &H4000
Const DDL_EXCLUSIVE = &H8000
Const DDL_POSTMSGS = &H2000

Dim DirName As String

Private Sub Form_Load()
   Dim StartDir  As String
   DirName = "c:\"
   Call SendMessage(List1.hwnd, LB_RESETCONTENT, 0, ByVal 0)
   Call SendMessage(List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal DirName & "*.*")
   Call SendMessage(List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_ARCHIVE, ByVal DirName & "*.*")
   Me.Caption = DirName
End Sub

Private Sub List1_DblClick()
  Dim sName As String, lRet As Long
  sName = List1.List(List1.ListIndex)
  If Left$(sName, 1) = "[" Then
     sName = Mid$(sName, 2, Len(sName) - 2)
     If sName = ".." Then
        DirName = GetParentDir(DirName)
     Else
        DirName = DirName & sName & "\"
     End If
     Me.Caption = DirName
     Call SendMessage(List1.hwnd, LB_RESETCONTENT, 0, ByVal 0)
     Call SendMessage(List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_DIRECTORY, ByVal DirName & "*.*")
     Call SendMessage(List1.hwnd, LB_DIR, DDL_EXCLUSIVE Or DDL_ARCHIVE, ByVal DirName & "*.*")
End If
End Sub

Private Function GetParentDir(sDir As String) As String
  For i = Len(sDir) - 1 To 1 Step -1
      If Mid$(sDir, i, 1) = "\" Then
         GetParentDir = Left$(sDir, i)
         Exit Function
      End If
  Next i
End Function

Run programm. First time listbox show you content of "c:\". Double click at any folder - you receive folders and files in it. You can go to another folder or return back clicing [..]

Cheers
PS You can add flags that you need to show system, hidden etc files