Visual Basic .NET - ListView Control

The VB.NET ListView control is a powerful tool for displaying lists of information, such as files or items in a database. It is very customizable and can be used to create a variety of user interfaces.

Common Properties:

  • Items: The collection of items in the list.
  • View: The view mode of the list (e.g. details, icons, etc.).
  • Columns: The collection of columns in the list.
  • SelectedItems: The collection of selected items in the list.
  • FullRowSelect: Determines whether clicking an item selects the entire row.
  • MultiSelect: Determines whether multiple items can be selected.
  • CheckBoxes: Determines whether checkboxes are displayed next to each item.

Common Methods:

  • Add: Adds a new item to the list.
  • Remove: Removes an item from the list.
  • Clear: Removes all items from the list.
  • Sort: Sorts the items in the list.
  • FindItemWithText: Searches for an item with the specified text.
  • FindItemWithText: Searches for an item with the specified text.

Common Events:

  • SelectedIndexChanged: Raised when the selected item or items in the list changes.
  • ItemChecked: Raised when the checkbox state of an item changes.
  • ColumnClick: Raised when the user clicks on a column header to sort the list.
  • ItemActivate: Raised when the user double-clicks on an item.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set the view mode to details
    ListView1.View = View.Details
    ' Add columns to the list
    ListView1.Columns.Add("Name")
    ListView1.Columns.Add("Size")
    ListView1.Columns.Add("Date Modified")
    ' Get the files in the directory
    Dim dir As New DirectoryInfo("C:\")
    Dim files() As FileInfo = dir.GetFiles()
    ' Add the files to the list
    For Each file As FileInfo In files
        Dim item As New ListViewItem(file.Name)
        item.SubItems.Add(file.Length.ToString())
        item.SubItems.Add(file.LastWriteTime.ToString())
        ListView1.Items.Add(item)
    Next
End Sub

This code sets the view mode to details and adds three columns to the list. It then gets the files in the C:\ directory and adds each file to the list as a new ListViewItem object with sub-items for the file size and date modified. The resulting list displays the files in a table-like format with columns for the file name, size, and date modified.

To add items to a ListView control, you can use the Items property of the control to access the ListViewItemCollection class, which represents the collection of items in the ListView. You can then add new items to the collection using the Add method.

Here's an example of how to add three items to a ListView control:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Add three items to the ListView control
    Dim item1 As New ListViewItem("Item 1")
    Dim item2 As New ListViewItem("Item 2")
    Dim item3 As New ListViewItem("Item 3")
    ListView1.Items.Add(item1)
    ListView1.Items.Add(item2)
    ListView1.Items.Add(item3)
End Sub

This code adds three items to the ListView1 control, each with a different text value ("Item 1", "Item 2", and "Item 3").

You can also add sub-items to each item using the SubItems property of the ListViewItem class. For example, to add a sub-item to the first item in the above example, you could use the following code:

item1.SubItems.Add("Sub-Item 1")

This would add a new sub-item to the first item with the text value "Sub-Item 1".

In addition to adding items programmatically, you can also add items at design time using the ListView control's "Items" editor. To open the editor, select the ListView control and click the "Items" property in the Properties window. This will open a dialog where you can add, remove, and modify items and sub-items in the ListView control.