Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

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.
0
Shroom
Asked:
Shroom
  • 2
  • 2
1 Solution
 
ArkCommented:
0
 
AshokKumarCommented:
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.
0
 
mcriderCommented:
Why not just use a DirListBox and a FileListBox??

Anyhow, here's the code to do it:

    Dim dirName As String
    Dim lBuf As String
    Dim lPath As String
   
    lPath = "C:\"
   
    Combo1.Clear
    List1.Clear
    dirName = Dir(lPath, vbDirectory)
    Do While Not dirName = ""
        lBuf = IIf(Right$(lPath, 1) = "\", lPath, lPath & "\") & dirName
        If GetAttr(lBuf) = vbDirectory Then
            Combo1.AddItem dirName
        Else
            List1.AddItem dirName
        End If
        dirName = Dir
    Loop


Cheers!®©
0
 
mcriderCommented:
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!®©
0
 
ArkCommented:
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
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now