Solved

VB6 Display directory content into a list box

Posted on 2007-03-28
5
2,547 Views
Last Modified: 2010-08-05
I know this is a simple one, but I for some reason can not get through it. I am trying to list the contents of a directory in VB6. I am trying to list it on a list box in checkbox mode so that I can later delete any files that are check can some one help me with this. Thank you.
0
Comment
Question by:leotron
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18811054
what contents? the files, the directories?
are you aware that VB comes with a filelistbox control, where all you need to do is put the path and the filter in.

instead of the checkbox, use the multiselect open.
0
 
LVL 1

Author Comment

by:leotron
ID: 18811091
The thing is that I am simply tring to make it as simple as possible for my end users. and I think that the check box method would be easier for them. and I would like to list all files inside a particular directory.  and have the files listed in a list box so that the end user simply clicks on the check mark and that's it.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18811112
assuming your listbox is called lstFiles, and is configured to checkbox display

lstFiles.Clear

strFile = Dir("YourFolder\")
while strFile <> empty
  lstFiles.Add strFile
  strFile = Dir
wend

0
 
LVL 12

Accepted Solution

by:
jkaios earned 500 total points
ID: 18812118
>>  am trying to list it on a list box in checkbox mode so that I can later delete any files that are checked.
I believe that a standard VB6 ListBox control cannot easily accomplish this because the standard VB6 ListBox doesn't have the "check" property.

The more preferable way is to do this is to use the ListView control available in Microsoft Windows Common Control 6.0

1. Add Microsoft Windows Common Controls 6.0 (MSCOMCTL.OCX) to your project
2. Select the ListView from the Toolbox
3. Copy and paste the following code to the form

'===============================================================================
Option Explicit

Private Sub Form_Load()
   populateListView "C:\Temp"
End Sub

Private Sub populateListView(sPath As String)

   Dim oItem As MSComctlLib.ListItem
   Dim sFile As String
   
   With ListView1
      .View = lvwReport
      .Sorted = False
      .LabelEdit = lvwManual
      .FullRowSelect = True
      .Checkboxes = True
      .HideSelection = False
      '---------------------
      .ColumnHeaders.Clear
      .ListItems.Clear
      '---------------------
      .ColumnHeaders.Add , , "?", 300
      .ColumnHeaders.Add , , "File Name", 3000
      .ColumnHeaders.Add , , "File Size", 1000, lvwColumnRight
      .ColumnHeaders.Add , , "Date Modified", 2000
   End With
   
   If Right(sPath, 1) <> "\" Then
      sPath = sPath & "\"
   End If
   
   sFile = Dir(sPath & "*.*")
   
   Do Until (sFile = "")
      DoEvents
      Set oItem = ListView1.ListItems.Add
      oItem.Tag = sPath & sFile
      oItem.SubItems(1) = sFile
      oItem.SubItems(2) = FormatNumber(FileLen(sPath & sFile), 0)
      oItem.SubItems(3) = FileDateTime(sPath & sFile)
      sFile = Dir()
   Loop
   
   Me.Caption = ListView1.ListItems.Count & " files(s) found"
   
End Sub
'===============================================================================
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now