ListView Demo : ListView « GUI « VB.Net Tutorial






ListView Demo
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class ListViewIllustration
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
        Dim intWidth As Integer
        Dim objItem As ListViewItem

        'Set the default view.
        ListView1.View = View.Details
        optDetails.Checked = True

        'Add the column headers.
        intWidth = ListView1.Width - 5
        ListView1.Columns.Add("Name", CInt(intWidth / 4))
        ListView1.Columns.Add("Address", CInt(intWidth / 4))
        ListView1.Columns.Add("Phone", CInt(intWidth / 4))
        ListView1.Columns.Add("FAX", CInt(intWidth / 4))

        'Add some list view items.
        objItem = ListView1.Items.Add("AAAAA")
        With objItem
            .SubItems.Add("123 Main St.")
            .SubItems.Add("555-555-5555")
            .SubItems.Add("555-555-5555")
            .ImageIndex = 0
        End With

        objItem = ListView1.Items.Add("BBBBB")
        With objItem
            .SubItems.Add("456 Main St.")
            .SubItems.Add("555-555-5555")
            .SubItems.Add("555-555-5555")
            .ImageIndex = 0
        End With

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim strItemText As String
        Dim objItem As ListViewItem

        'Add some list view items.
        strItemText = "name:"
        objItem = ListView1.Items.Add(strItemText)
        With objItem
            .SubItems.Add("123 Some St.")
            .SubItems.Add("555-555-5555")
            .SubItems.Add("555-555-5555")
            .ImageIndex = 0
        End With

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ListView1.Items.Clear()
    End Sub
    
    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click

        Dim objListItem As ListViewItem

        For Each objListItem In ListView1.SelectedItems
            objListItem.Remove()
        Next objListItem

    End Sub

    Private Sub btnDisplayItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayItem.Click

        Dim strMessage As String
        Dim objListItem As ListViewItem

        If ListView1.SelectedItems.Count > 0 Then
            objListItem = ListView1.SelectedItems(0)
            With objListItem
                strMessage = "NAME: " & .Text & vbCrLf & _
                             "ADDRESS: " & .SubItems(1).Text & vbCrLf & _
                             "PHONE: " & .SubItems(2).Text & vbCrLf & _
                             "FAX: " & .SubItems(3).Text
            End With
            MsgBox(strMessage)
        End If

    End Sub
   
    Private Sub optLargeIcon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optLargeIcon.CheckedChanged
        ListView1.View = View.LargeIcon
    End Sub

    Private Sub optSmallIcon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optSmallIcon.CheckedChanged
        ListView1.View = View.SmallIcon
    End Sub

    Private Sub optList_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optList.CheckedChanged
        ListView1.View = View.List
    End Sub

    Private Sub optTile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optTile.CheckedChanged
        ListView1.View = View.Tile
    End Sub

    Private Sub optDetails_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDetails.CheckedChanged
        ListView1.View = View.Details
    End Sub

End Class
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.btnPopulate = New System.Windows.Forms.Button
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.optDetails = New System.Windows.Forms.RadioButton
        Me.optTile = New System.Windows.Forms.RadioButton
        Me.optList = New System.Windows.Forms.RadioButton
        Me.optSmallIcon = New System.Windows.Forms.RadioButton
        Me.optLargeIcon = New System.Windows.Forms.RadioButton
        Me.btnAdd = New System.Windows.Forms.Button
        Me.btnRemove = New System.Windows.Forms.Button
        Me.btnClear = New System.Windows.Forms.Button
        Me.btnDisplayItem = New System.Windows.Forms.Button
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'ListView1
        '
        Me.ListView1.FullRowSelect = True
        Me.ListView1.Location = New System.Drawing.Point(17, 18)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(342, 364)
        Me.ListView1.TabIndex = 0
        Me.ListView1.UseCompatibleStateImageBehavior = False
        '
        'btnPopulate
        '
        Me.btnPopulate.Location = New System.Drawing.Point(366, 239)
        Me.btnPopulate.Name = "btnPopulate"
        Me.btnPopulate.Size = New System.Drawing.Size(161, 23)
        Me.btnPopulate.TabIndex = 2
        Me.btnPopulate.Text = "Populate"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.optDetails)
        Me.GroupBox1.Controls.Add(Me.optTile)
        Me.GroupBox1.Controls.Add(Me.optList)
        Me.GroupBox1.Controls.Add(Me.optSmallIcon)
        Me.GroupBox1.Controls.Add(Me.optLargeIcon)
        Me.GroupBox1.Location = New System.Drawing.Point(370, 13)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(156, 166)
        Me.GroupBox1.TabIndex = 1
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Views"
        '
        'optDetails
        '
        Me.optDetails.AutoSize = True
        Me.optDetails.Location = New System.Drawing.Point(30, 126)
        Me.optDetails.Name = "optDetails"
        Me.optDetails.Size = New System.Drawing.Size(57, 17)
        Me.optDetails.TabIndex = 4
        Me.optDetails.Text = "Details"
        '
        'optTile
        '
        Me.optTile.AutoSize = True
        Me.optTile.Location = New System.Drawing.Point(30, 102)
        Me.optTile.Name = "optTile"
        Me.optTile.Size = New System.Drawing.Size(42, 17)
        Me.optTile.TabIndex = 3
        Me.optTile.Text = "Tile"
        '
        'optList
        '
        Me.optList.AutoSize = True
        Me.optList.Location = New System.Drawing.Point(30, 78)
        Me.optList.Name = "optList"
        Me.optList.Size = New System.Drawing.Size(41, 17)
        Me.optList.TabIndex = 2
        Me.optList.Text = "List"
        '
        'optSmallIcon
        '
        Me.optSmallIcon.AutoSize = True
        Me.optSmallIcon.Location = New System.Drawing.Point(30, 54)
        Me.optSmallIcon.Name = "optSmallIcon"
        Me.optSmallIcon.Size = New System.Drawing.Size(79, 17)
        Me.optSmallIcon.TabIndex = 1
        Me.optSmallIcon.Text = "Small Icons"
        '
        'optLargeIcon
        '
        Me.optLargeIcon.AutoSize = True
        Me.optLargeIcon.Location = New System.Drawing.Point(30, 30)
        Me.optLargeIcon.Name = "optLargeIcon"
        Me.optLargeIcon.Size = New System.Drawing.Size(81, 17)
        Me.optLargeIcon.TabIndex = 0
        Me.optLargeIcon.Text = "Large Icons"
        '
        'btnAdd
        '
        Me.btnAdd.Location = New System.Drawing.Point(365, 269)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(161, 23)
        Me.btnAdd.TabIndex = 3
        Me.btnAdd.Text = "Add"
        '
        'btnRemove
        '
        Me.btnRemove.Location = New System.Drawing.Point(366, 299)
        Me.btnRemove.Name = "btnRemove"
        Me.btnRemove.Size = New System.Drawing.Size(161, 23)
        Me.btnRemove.TabIndex = 4
        Me.btnRemove.Text = "Remove"
        '
        'btnClear
        '
        Me.btnClear.Location = New System.Drawing.Point(365, 329)
        Me.btnClear.Name = "btnClear"
        Me.btnClear.Size = New System.Drawing.Size(161, 23)
        Me.btnClear.TabIndex = 5
        Me.btnClear.Text = "Clear"
        '
        'btnDisplayItem
        '
        Me.btnDisplayItem.Location = New System.Drawing.Point(365, 359)
        Me.btnDisplayItem.Name = "btnDisplayItem"
        Me.btnDisplayItem.Size = New System.Drawing.Size(161, 23)
        Me.btnDisplayItem.TabIndex = 6
        Me.btnDisplayItem.Text = "Display Item"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(539, 394)
        Me.Controls.Add(Me.btnDisplayItem)
        Me.Controls.Add(Me.btnClear)
        Me.Controls.Add(Me.btnRemove)
        Me.Controls.Add(Me.btnAdd)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.btnPopulate)
        Me.Controls.Add(Me.ListView1)
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "ListView"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents btnPopulate As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents optDetails As System.Windows.Forms.RadioButton
    Friend WithEvents optTile As System.Windows.Forms.RadioButton
    Friend WithEvents optList As System.Windows.Forms.RadioButton
    Friend WithEvents optSmallIcon As System.Windows.Forms.RadioButton
    Friend WithEvents optLargeIcon As System.Windows.Forms.RadioButton
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnRemove As System.Windows.Forms.Button
    Friend WithEvents btnClear As System.Windows.Forms.Button
    Friend WithEvents btnDisplayItem As System.Windows.Forms.Button
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList

End Class








14.15.ListView
14.15.1.ListView DemoListView Demo
14.15.2.Creating List Views in CodeCreating List Views in Code
14.15.3.Add columns and rows to a ListViewAdd columns and rows to a ListView
14.15.4.Change ListView state: LargeIcon, SmallIcon, List and DetailChange ListView state: LargeIcon, SmallIcon, List and Detail
14.15.5.Add and delete ListView ItemAdd and delete ListView Item
14.15.6.ListView Item Check eventListView Item Check event
14.15.7.Displaying directories and their contents in ListViewDisplaying directories and their contents in ListView
14.15.8.ListView ItemActivate eventListView ItemActivate event
14.15.9.ListView with CheckBox cellListView with CheckBox cell
14.15.10.Add file name to ListViewAdd file name to ListView
14.15.11.Runtime ListViewRuntime ListView