Visual Basic List API Routines
SendMessage: Instantly Fill a List with Files, Folders or Drives
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SendMessage: Instantly Fill a Combo with Files, Folders or Drives
     
 Prerequisites
None.

vbnslistboxdir.gif (7894 bytes)This routine demonstrated using the API and a standard listbox to duplicate the functionality of a FileList Box.  By using this method over the FileListBox, you can control the files, directories and drives displayed. 

Note however that the constant value for 'READWRITE' is 0, the same as the VB constant 'Archive'. In VB you can not mask out the archive files to only return hidden files (for example).  But by using the API value DDL_EXCLUSIVE Or'd with the type to be listed, you have control over the display of files.

The added bonus is that this method is instantaneous.

 BAS Module Code
None.

 Form Code
Create a form containing six command buttons in a control array (Command1(0) - Command1(5)), a label (Label1) and a listbox (List1). Add the following code to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright 1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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

Private Const LB_DIR = &H18D
Private Const LB_RESETCONTENT = &H184

Private Const DDL_READWRITE = &H0
Private Const DDL_READONLY = &H1
Private Const DDL_HIDDEN = &H2
Private Const DDL_SYSTEM = &H4
Private Const DDL_DIRECTORY = &H10
Private Const DDL_ARCHIVE = &H20
Private Const DDL_DRIVES = &H4000
Private Const DDL_EXCLUSIVE = &H8000&
Private Const DDL_POSTMSGS = &H2000
Private Const DDL_FLAGS = DDL_ARCHIVE Or DDL_DIRECTORY



Private Sub Form_Load()

   Command1(0).Caption = "normal files"
   Command1(1).Caption = "normal files and folders"
   Command1(2).Caption = "hidden files"
   Command1(3).Caption = "system files"
   Command1(4).Caption = "folders"
   Command1(5).Caption = "drives"

End Sub


Private Sub Command1_Click(Index As Integer)

   Dim found As Long
   Dim DDL_FLAGS As Long
   Dim sPath As String
   
   sPath = "c:\winnt\*.*"
   
   Select Case Index
      Case 0: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_ARCHIVE
      Case 1: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_ARCHIVE Or DDL_DIRECTORY
      Case 2: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_HIDDEN
      Case 3: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_SYSTEM
      Case 4: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_DIRECTORY
      Case 5: DDL_FLAGS = DDL_EXCLUSIVE Or DDL_DRIVES
      Case Else
   End Select
   
  'clear and populate the listbox
   Call SendMessage(List1.hwnd, LB_RESETCONTENT, 0, ByVal 0)
   found = SendMessage(List1.hwnd, LB_DIR, DDL_FLAGS, ByVal sPath)

   Label1.Caption = found + 1 & " " & Command1(Index).Caption & " found."
   
End Sub
 Comments
By changing the value of the DDL_FLAGS parameter to include or exclude specific DDL_* constants, you determine the type of file structure to include into the list Be sure to set a correct path for your system in the command event..

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright 1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter