none
fILE lIST bOX RRS feed

  • Question

  • I'm using VB 2010 Basic Express and the tool box does not show the FileListBox, control.  What am I missing to get this control?

    I included in module1 IMPORT System IO and other imports.  Also the Power Packs.


    Martin R Pettersson

    Sunday, March 22, 2020 4:45 PM

All replies

  • It is marked obsolete : FileListBox Class
    Sunday, March 22, 2020 4:50 PM
  • Darn.  Thanks

    Martin R Pettersson

    Sunday, March 22, 2020 5:27 PM
  • You can make your own. The following would be considered a base

    Imports System.ComponentModel
    Imports System.IO
    Public Class FileListBox
        Inherits ListBox
    
        Public Sub New()
            FileList = New List(Of String)
            Sorted = True
        End Sub
        ''' <summary>
        ''' Set this to the folder to get files
        ''' </summary>
        ''' <returns></returns>
        <Category("Behavior")>
        <Description("Folder to get files")>
        Public Property SelectedFolder() As String
        ''' <summary>
        ''' Optionally access files via here rather than casting the DataSource
        ''' property
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False)>
        Public Property FileList() As List(Of String)
        ''' <summary>
        ''' Get selected folder\file name
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>
        ''' Additional to this code you might like to check if 
        ''' * There are items in the DataSource
        ''' * The folder and also the file exists
        ''' </remarks>
        <Browsable(False)>
        Public ReadOnly Property SelectedFile() As String
            Get
                Return Path.Combine(SelectedFolder, Text)
            End Get
        End Property
        ''' <summary>
        ''' Get FileInfo for selected file at runtime
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False)>
        Public ReadOnly Property SelectedFileInfo() As FileInfo
            Get
                Return New FileInfo(Path.Combine(SelectedFolder, Text))
            End Get
        End Property
        ''' <summary>
        ''' Read all files in selected folder by not passing a parameter 
        ''' </summary>
        Public Sub ReadFolder(Optional wildCard As String = "*.*")
    
            If Not String.IsNullOrWhiteSpace(SelectedFolder) AndAlso
               Directory.Exists(SelectedFolder) Then
    
                FileList = Directory.GetFiles(SelectedFolder, wildCard).
                    Select(Function(fullName) Path.GetFileName(fullName)).ToList()
    
                DataSource = FileList
    
            End If
    
        End Sub
        ''' <summary>
        ''' Clear both DataSource and internal FileList property
        ''' </summary>
        Public Sub Clear()
    
            DataSource = Nothing
            FileList = Nothing
    
        End Sub
    
    End Class
    
    

    Load with a specific folder with all files

    ' this can be set in the property window
    FileListBox1.SelectedFolder = "C:\SomeFolder"
    FileListBox1.ReadFolder()

    Load with specific extension

    FileListBox1.ReadFolder("*.docx")

    If multiple extensions are needed code would be simple to do this

    Get the selected file name with folder name

    FileListBox1.SelectedFile

    Get FileInfo for the selected file, here getting the file size

    FileListBox1.SelectedFileInfo.Length
    As I said this is a base code sample that can be expanded.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 5:56 PM
    Moderator
  • Thank you Karen.

    Martin R Pettersson

    Sunday, March 22, 2020 6:23 PM
  • Thank you Karen.

    Martin R Pettersson

    Your welcome, since I have time today because of world events I went a bit farther by adding an event and a new property were both were in the older FileListBox and also changed the name to ListBoxFile. Placed all code below into a class project and added a reference to Windows.Forms to the class project.

    Delegate for path changed event

    Public Module DelegatesModule
        Public Delegate Sub PathChangedEventHandler(
            sender As Object, e As PathChangedArgs)
    End Module

    Arguments for above delegate

    Public Class PathChangedArgs
        Inherits EventArgs
    
        Private _folderName As String
        Private _currentFolderName As String
        Public Sub New(folderName As String, currentFolderName As String)
            _folderName = folderName
            _currentFolderName = currentFolderName
        End Sub
        ''' <summary>
        ''' Changed folder when PathChangedEventHandler has been raised
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property FolderName As String
            Get
                Return _folderName
            End Get
        End Property
        ''' <summary>
        ''' Current folder to use before change PathChangedEventHandler
        ''' is raised
        ''' </summary>
        ''' <returns></returns>
        Public ReadOnly Property CurrentFolderName As String
            Get
                Return If(String.IsNullOrWhiteSpace(_currentFolderName), "", _currentFolderName)
            End Get
        End Property
    
    End Class

    Revised control.

    Imports System.ComponentModel
    Imports System.IO
    Imports System.Windows.Forms
    
    Public Class ListBoxFiles
        Inherits ListBox
    
        Private _SelectedFolder As String
        Public Event PathChangedEventHandler As PathChangedEventHandler
    
        Public Sub New()
            FileList = New List(Of String)
            Sorted = True
        End Sub
        ''' <summary>
        ''' Set this to the folder to get files
        ''' </summary>
        ''' <returns></returns>
        <Category("Behavior")>
        <Description("Folder to get files")>
        Public Property SelectedFolder() As String
            Get
                Return _SelectedFolder
            End Get
            Set
                Dim oldValue = _SelectedFolder
                _SelectedFolder = Value
                RaiseEvent PathChangedEventHandler(Me, New PathChangedArgs(Value, oldValue))
            End Set
        End Property
    
        ''' <summary>
        ''' Optionally access files via here rather than casting the DataSource
        ''' property
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False)>
        Public Property FileList() As List(Of String)
        ''' <summary>
        ''' Get selected folder\file name
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>
        ''' Additional to this code you might like to check if 
        ''' * There are items in the DataSource
        ''' * The folder and also the file exists
        ''' </remarks>
        <Browsable(False)>
        Public ReadOnly Property SelectedFile() As String
            Get
                Return Path.Combine(SelectedFolder, Text)
            End Get
        End Property
        <Category("Behavior")>
        <Description("A string expression indicating a file specification, such as ""*.docx"" leave empty for all files.")>
        Public Property Pattern() As String
        ''' <summary>
        ''' Get FileInfo for selected file at runtime
        ''' </summary>
        ''' <returns></returns>
        <Browsable(False)>
        Public ReadOnly Property SelectedFileInfo() As FileInfo
            Get
                Return New FileInfo(Path.Combine(SelectedFolder, Text))
            End Get
        End Property
        ''' <summary>
        ''' Read all files in selected folder by not passing a parameter 
        ''' </summary>
        Public Sub ReadFolder()
    
            If Not String.IsNullOrWhiteSpace(SelectedFolder) AndAlso
               Directory.Exists(SelectedFolder) Then
    
                FileList = Directory.GetFiles(SelectedFolder,
                    If(String.IsNullOrWhiteSpace(Pattern), "*.*", Pattern)).
                    Select(Function(fullName) Path.GetFileName(fullName)).ToList()
    
                DataSource = FileList
    
            End If
    
        End Sub
        ''' <summary>
        ''' Clear both DataSource and internal FileList property
        ''' </summary>
        Public Sub Clear()
    
            DataSource = Nothing
            FileList = Nothing
    
        End Sub
    
    End Class
    
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 6:50 PM
    Moderator
  • You can also use DlgDirList which fills automatically the Listbox with files

    (I had to use a Win32 Listbox as it crashes on exit with a .NET Listbox on my configuration...)

        <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
        Public Shared Function DlgDirList(hDlg As IntPtr, <[In](), MarshalAs(UnmanagedType.LPWStr)> lpPathSpec As String, nIDListBox As Integer, nIDStaticPath As Integer, uFileType As UInteger) As Integer
        End Function
    
        Public Const DDL_READWRITE As Integer = &H0
        Public Const DDL_READONLY As Integer = &H1
        Public Const DDL_HIDDEN As Integer = &H2
        Public Const DDL_SYSTEM As Integer = &H4
        Public Const DDL_DIRECTORY As Integer = &H10
        Public Const DDL_ARCHIVE As Integer = &H20
    
        Public Const DDL_POSTMSGS As Integer = &H2000
        Public Const DDL_DRIVES As Integer = &H4000
        Public Const DDL_EXCLUSIVE As Integer = &H8000

    or IExplorerBrowser

    for an Explorer-like GUI (ListView, not Listbox):


    • Edited by Castorix31 Sunday, March 22, 2020 8:19 PM
    Sunday, March 22, 2020 7:46 PM