Search code examples
vb.netlistviewimagelist

How to display icon in ListView in VB.Net


Can you help me to display icon view from the files i get from a directory

Here is my code.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SearchDir("g:\")
    End Sub

    Public Sub SearchDir(ByVal sDir As String)
        Dim fil As String

        Try
            For Each dir As String In Directory.GetDirectories(sDir)
                For Each fil In Directory.GetFiles(dir, " *.doc ")
                    ListView1.Items.Add(fil)
                Next
                SearchDir(dir)
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

this gives me a result, but in form of string displaying its path


Solution

  • First of all, you need to add ImageList tool from Visual Studio Toolbox. Then select its properties and select the images you need to work with.

    ImageList Tool Configuration for ImageList

    After that, you need to use the corresponding given serial-wise IDs by ImageList in your ListView code as follows:

    Declaration:

    Private lView As ListViewItem ' listView's lView (not I-view)
    
    lView = ListView1.Items.Add("Special iconic thing", 0) ' 0 = my icon ID in ImageList
    

    You should get the similar to my output:

    Results

    Hope it works for you.