Solved

VB6 Display directory content into a list box

Posted on 2007-03-28
5
2,575 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 143

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 143

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

856 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