CodeItBetter Programming Another VB Programming Blog

How to put Drives, Directories And Files In One List Box

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'File/Folder Handling - How to put Drives, Directories And Files In One List Box

Option Explicit
 
'Add a List Box, a Drive List Box, a Dir List Box, a File List Box and a Label to your form.
'Insert the following code:

Function StripPath(T$) As String
    Dim x%, ct%
    StripPath$ = T$
    x% = InStr(T$, "\")
    Do While x%
        ct% = x%
        x% = InStr(ct% + 1, T$, "\")
    Loop
    If ct% > 0 Then StripPath$ = Mid$(T$, ct% + 1)
End Function
 
Sub UpdatePath()
    Dim I As Integer, D As Integer, J As Integer, K As Integer
    For D = 0 To List1.ListCount - 1
        List1.RemoveItem "0"
    Next D
    If Not Right(Dir1.List(-1), 1) = "\" Then
        List1.AddItem "[^] .."
    End If
    For I = 0 To Dir1.ListCount - 1
        List1.AddItem "[\] " & StripPath(Dir1.List(I))
    Next I
    For J = 0 To File1.ListCount - 1
        List1.AddItem "[*] " & File1.List(J)
    Next J
    For K = 0 To Drive1.ListCount - 1
        List1.AddItem "[o] " & Drive1.List(K)
    Next K
    Label1.Caption = Dir1.Path
End Sub
 
Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
End Sub
 
Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    UpdatePath
End Sub
 
Private Sub Form_Load()
    Drive1.Visible = False
    File1.Visible = False
    Dir1.Visible = False
    UpdatePath
    Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
End Sub
 
Private Sub List1_DblClick()
    If Right(List1.Text, 2) = ".." Then
        Dir1.Path = Dir1.Path & "\.."
    ElseIf Left(List1.Text, 3) = "[\]" Then
        If Right(Dir1.List(-1), 1) = "\" Then
            Dir1.Path = Dir1.Path & Right(List1.Text, Len(List1.Text) - 4)
        Else
            Dir1.Path = Dir1.Path & "\" & Right(List1.Text, Len(List1.Text) - 4)
        End If
    ElseIf Left(List1.Text, 3) = "[o]" Then
        Drive1.Drive = Right(Left(List1.Text, 6), 2)
    Else
        MsgBox "File " &  chr ( 34) & Right(List1.Text, Len(List1.Text) - 4) &  chr ( 34) & " was chosen.", vbInformation, "File Chosen"
    End If
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.