ListView in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the ListView control


ListView

This control displays a list of data items and subitems, and it does that in a five different ways:

Details - displays a small icon and the item's and subitem's text.

LargeIcon - displays large icon just above the item's text, and the entries are arranged from left to right until they wrap to the next row.

SmallIcon - displays small icon to the left of the items text, and the entries are arranged from left to right until they wrap to the next row.

List - displays small icon to the left of the item's text, and each item is placed in a new row.

Tile - displays large icon to the left of the item's text, and the entries are arranged from left to right until they wrap to the next row.

view

Don't forget to set the properties LargeImageList and SmallImageList to ImageList controls we added previously:

image list

    Dim dir As New IO.DirectoryInfo(path)
    ' populate the listbox controls
    For Each file As FileInfo In dir.GetFiles
        ' set the image index according extension
        Dim imageindex As Integer = 0
        Dim filetype As String = String.Empty
        Select Case file.Extension
            Case ".png", ".jpg", ".bmp", ".gif"
                filetype = "Image"
                imageindex = 1
            Case ".txt"
                filetype = "Text file"
                imageindex = 2
            Case Else
                ' an unknown format so we assign the document image/icon
                filetype = "Document"
                imageindex = 0
        End Select

        ' Details view
        Dim DetailsItem As ListViewItem = Details.Items.Add(file.Name, imageindex)
        DetailsItem.SubItems.Add(file.LastWriteTime)
        DetailsItem.SubItems.Add(filetype)
        DetailsItem.SubItems.Add(String.Format("{0:n2} KB", Convert.ToDouble(file.Length / 1024)))

        ' LargeIcon view
        Dim LargeIconViewItem As ListViewItem = LargeIcon.Items.Add(file.Name, imageindex)

        ' SmallIcon view
        Dim SmallIconViewItem As ListViewItem = SmallIcon.Items.Add(file.Name, imageindex)

        ' Tile view
        Dim TileViewItem As ListViewItem = Tile.Items.Add(file.Name, imageindex)

        ' List view
        Dim ListViewItem As ListViewItem = List.Items.Add(file.Name, imageindex)
    Next


If you set the ListView control's CheckBoxes property to True, the control displays check boxes next to its items and you can use the CheckedIndices or CheckedItems collections to see which items the user has selected.

    For Each item As ListViewItem In Details.CheckedItems
        MessageBox.Show(item.SubItems(0).Text)
    Next

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.