Solved

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

Posted on 2000-03-13
5
142 Views
Last Modified: 2010-05-02
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
Comment
Question by:Shroom
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Ark
Comment Utility
0
 

Expert Comment

by:AshokKumar
Comment Utility
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
 
LVL 14

Accepted Solution

by:
mcrider earned 100 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now