Solved

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

Posted on 2000-03-13
5
143 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
ID: 2614892
0
 

Expert Comment

by:AshokKumar
ID: 2614907
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
ID: 2614913
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
ID: 2614926
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
ID: 2615079
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 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

12 Experts available now in Live!

Get 1:1 Help Now