VB.Net Tutorial/GUI/ListView — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:57, 26 мая 2010
Содержание
- 1 Add and delete ListView Item
- 2 Add columns and rows to a ListView
- 3 Add file name to ListView
- 4 Change ListView state: LargeIcon, SmallIcon, List and Detail
- 5 Creating List Views in Code
- 6 Displaying directories and their contents in ListView
- 7 ListView Demo
- 8 ListView ItemActivate event
- 9 ListView Item Check event
- 10 ListView with CheckBox cell
- 11 Runtime ListView
Add and delete ListView Item
<source lang="vbnet">Imports System.Windows.Forms public class ListViewState
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents ImageList1 As System.Windows.Forms.ImageList Friend WithEvents ImageList2 As System.Windows.Forms.ImageList Friend WithEvents ImageList3 As System.Windows.Forms.ImageList Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.ListView1 = New System.Windows.Forms.ListView Me.TextBox1 = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5}) Me.MenuItem1.Text = "View" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Large Icon" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Small Icon" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "List" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "Detail" " "ListView1 " Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top Me.ListView1.Location = New System.Drawing.Point(0, 0) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(328, 128) Me.ListView1.TabIndex = 0 " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(88, 168) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(80, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Label1 " Me.Label1.Location = New System.Drawing.Point(48, 168) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(40, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "Project" " "Label2 " Me.Label2.Location = New System.Drawing.Point(16, 200) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(72, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "No" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(88, 200) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(80, 20) Me.TextBox2.TabIndex = 4 Me.TextBox2.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(192, 184) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 5 Me.Button1.Text = "Add" " "Button3 " Me.Button3.Location = New System.Drawing.Point(120, 240) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(80, 24) Me.Button3.TabIndex = 9 Me.Button3.Text = "Delete" " "ColumnHeader1 " Me.ColumnHeader1.Text = "File" " "ColumnHeader2 " Me.ColumnHeader2.Text = "Size" " "ColumnHeader3 " Me.ColumnHeader3.Text = "Change Date" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 297) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListView1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click ListView1.View = View.LargeIcon End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click ListView1.View = View.SmallIcon End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click ListView1.View = View.List End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click ListView1.View = View.Details End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ListView1.FocusedItem.Remove() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.Items.Add("new", 2) End Sub
End Class</source>
Add columns and rows to a ListView
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AddColumnToListView
public Shared Sub Main Application.Run(New Form2) End Sub
End class Public Class Form2
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents ListView2 As System.Windows.Forms.ListView Friend WithEvents Button3 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.ListView1 = New System.Windows.Forms.ListView() Me.Button2 = New System.Windows.Forms.Button() Me.ListView2 = New System.Windows.Forms.ListView() Me.Button3 = New System.Windows.Forms.Button() Me.SuspendLayout() " "Button1 " Me.Button1.Location = New System.Drawing.Point(32, 8) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Get Files" " "ListView1 " Me.ListView1.Location = New System.Drawing.Point(32, 40) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(480, 56) Me.ListView1.TabIndex = 1 " "Button2 " Me.Button2.Location = New System.Drawing.Point(32, 120) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(80, 24) Me.Button2.TabIndex = 2 Me.Button2.Text = "Add Item" " "ListView2 " Me.ListView2.Location = New System.Drawing.Point(32, 160) Me.ListView2.Name = "ListView2" Me.ListView2.Size = New System.Drawing.Size(504, 97) Me.ListView2.TabIndex = 3 Me.ListView2.View = System.Windows.Forms.View.Details " "Button3 " Me.Button3.Location = New System.Drawing.Point(120, 120) Me.Button3.Name = "Button3" Me.Button3.TabIndex = 4 Me.Button3.Text = "Add Column" " "Form2 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(712, 429) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button3, Me.ListView2, Me.Button2, Me.ListView1, Me.Button1}) Me.Name = "Form2" Me.Text = "ListView Example" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.Columns.Clear() ListView1.Items.Clear() ListView1.View = View.Details ListView1.Columns.Add("Filename", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Extension", 50, HorizontalAlignment.Left) ListView1.Columns.Add("Bytes", 50, HorizontalAlignment.Right) Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("c:\") Dim files() As System.IO.FileInfo = DI.GetFiles Dim file As System.IO.FileInfo Dim li As ListViewItem For Each file In files li = ListView1.Items.Add(file.Name) li.SubItems.Add(file.Extension) li.SubItems.Add(file.Length) Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ListView2.Items.Add("boo") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ListView2.Columns.Add("mycolumn", 20, HorizontalAlignment.Center) End Sub
End Class</source>
Add file name to ListView
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AddImageFileToListView
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListView1 = New System.Windows.Forms.ListView() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.SuspendLayout() " "ListView1 " Me.ListView1.Location = New System.Drawing.Point(8, 16) Me.ListView1.MultiSelect = False Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(304, 232) Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.Details " "PictureBox1 " Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.PictureBox1.Location = New System.Drawing.Point(320, 16) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(600, 568) Me.PictureBox1.TabIndex = 1 Me.PictureBox1.TabStop = False " "Button1 " Me.Button1.Location = New System.Drawing.Point(8, 256) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(64, 24) Me.Button1.TabIndex = 2 Me.Button1.Text = "New Path:" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(8, 288) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(256, 20) Me.TextBox1.TabIndex = 3 Me.TextBox1.Text = "c:\winnt" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(804, 585) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.Button1, Me.PictureBox1, Me.ListView1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged PictureBox1.Image = Image.FromFile(ListView1.FocusedItem.Text) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.Columns.Clear() ListView1.View = View.Details ListView1.Columns.Add("Filename", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Extension", 50, HorizontalAlignment.Left) ListView1.Columns.Add("Bytes", 50, HorizontalAlignment.Right) Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("c:\") Dim files() As System.IO.FileInfo = DI.GetFiles Dim file As System.IO.FileInfo For Each file In files Select Case file.Extension Case ".jpeg", ".gif", ".bmp", ".jpg", ".tif", ".tiff", ".avi" Dim li As New ListViewItem(file.FullName) li.SubItems.Add(file.Extension) li.SubItems.Add(file.Length) ListView1.Items.Add(li) End Select Next End Sub
End Class</source>
Change ListView state: LargeIcon, SmallIcon, List and Detail
<source lang="vbnet">Imports System.Windows.Forms public class ListViewState
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents ImageList1 As System.Windows.Forms.ImageList Friend WithEvents ImageList2 As System.Windows.Forms.ImageList Friend WithEvents ImageList3 As System.Windows.Forms.ImageList Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.ListView1 = New System.Windows.Forms.ListView Me.TextBox1 = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.TextBox2 = New System.Windows.Forms.TextBox Me.Button1 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5}) Me.MenuItem1.Text = "View" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Large Icon" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Small Icon" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "List" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "Detail" " "ListView1 " Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top Me.ListView1.Location = New System.Drawing.Point(0, 0) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(328, 128) Me.ListView1.TabIndex = 0 " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(88, 168) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(80, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "" " "Label1 " Me.Label1.Location = New System.Drawing.Point(48, 168) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(40, 24) Me.Label1.TabIndex = 2 Me.Label1.Text = "Project" " "Label2 " Me.Label2.Location = New System.Drawing.Point(16, 200) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(72, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "No" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(88, 200) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(80, 20) Me.TextBox2.TabIndex = 4 Me.TextBox2.Text = "" " "Button1 " Me.Button1.Location = New System.Drawing.Point(192, 184) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 5 Me.Button1.Text = "Add" " "Button3 " Me.Button3.Location = New System.Drawing.Point(120, 240) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(80, 24) Me.Button3.TabIndex = 9 Me.Button3.Text = "Delete" " "ColumnHeader1 " Me.ColumnHeader1.Text = "File" " "ColumnHeader2 " Me.ColumnHeader2.Text = "Size" " "ColumnHeader3 " Me.ColumnHeader3.Text = "Change Date" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 297) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListView1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click ListView1.View = View.LargeIcon End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click ListView1.View = View.SmallIcon End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click ListView1.View = View.List End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click ListView1.View = View.Details End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ListView1.FocusedItem.Remove() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.Items.Add("new", 2) End Sub
End Class</source>
Creating List Views in Code
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListViewCreationInCode
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Dim ListView1 As ListView Public Sub New() MyBase.New() Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.ResumeLayout(False) ListView1 = New ListView ListView1.Location = New Point(75, 90) ListView1.Size = New Size(150, 150) Controls.Add(ListView1) ListView1.Columns.Add("File Name", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Date", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Attribute", 100, HorizontalAlignment.Center) Dim ListItem1 As ListViewItem ListItem1 = ListView1.Items.Add("Item 1") Dim ListItem2 As ListViewItem ListItem2 = ListView1.Items.Add("Item 2") Dim ListItem3 As ListViewItem ListItem3 = ListView1.Items.Add("Item 3") Dim ListItem4 As ListViewItem ListItem4 = ListView1.Items.Add("Item 4") ListView1.View = View.SmallIcon End Sub
End Class</source>
Displaying directories and their contents in ListView
<source lang="vbnet">Imports System.IO Imports System.Windows.Forms public class LoadFileDirectoryInformationTreeView
public Shared Sub Main Application.Run(New FrmListView) End Sub
End class Public Class FrmListView
Inherits Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub " display labels for current location in directory tree Friend WithEvents lblCurrent As Label Friend WithEvents lblDisplay As Label " displays contents of current directory Friend WithEvents lvwBrowser As ListView " specifies images for file icons and folder icons Friend WithEvents ilsFileFolder As ImageList
"Required by the Windows Form Designer Private components As System.ruponentModel.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.ruponents = New System.ruponentModel.Container() Me.ilsFileFolder = New System.Windows.Forms.ImageList(Me.ruponents) Me.lvwBrowser = New System.Windows.Forms.ListView() Me.lblCurrent = New System.Windows.Forms.Label() Me.lblDisplay = New System.Windows.Forms.Label() Me.SuspendLayout() " "ilsFileFolder " Me.ilsFileFolder.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit Me.ilsFileFolder.ImageSize = New System.Drawing.Size(16, 16) Me.ilsFileFolder.TransparentColor = System.Drawing.Color.Transparent " "lvwBrowser " Me.lvwBrowser.Location = New System.Drawing.Point(16, 88) Me.lvwBrowser.Name = "lvwBrowser" Me.lvwBrowser.RightToLeft = System.Windows.Forms.RightToLeft.No Me.lvwBrowser.Size = New System.Drawing.Size(448, 232) Me.lvwBrowser.SmallImageList = Me.ilsFileFolder Me.lvwBrowser.TabIndex = 2 Me.lvwBrowser.View = System.Windows.Forms.View.List " "lblCurrent " Me.lblCurrent.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblCurrent.ForeColor = System.Drawing.SystemColors.WindowText Me.lblCurrent.Location = New System.Drawing.Point(16, 16) Me.lblCurrent.Name = "lblCurrent" Me.lblCurrent.Size = New System.Drawing.Size(112, 23) Me.lblCurrent.TabIndex = 0 Me.lblCurrent.Text = "Now in Directory:" " "lblDisplay " Me.lblDisplay.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblDisplay.ForeColor = System.Drawing.SystemColors.WindowText Me.lblDisplay.Location = New System.Drawing.Point(128, 16) Me.lblDisplay.Name = "lblDisplay" Me.lblDisplay.Size = New System.Drawing.Size(344, 56) Me.lblDisplay.TabIndex = 1 " "FrmListView " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(488, 341) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lvwBrowser, Me.lblDisplay, Me.lblCurrent}) Me.Name = "FrmListView" Me.Text = "ListViewTest" Me.ResumeLayout(False) End Sub
- End Region
Dim currentDirectory As String = Directory.GetCurrentDirectory() Private Sub lvwBrowser_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles lvwBrowser.Click If lvwBrowser.SelectedItems.Count <> 0 Then If lvwBrowser.Items(0).Selected Then Dim directoryObject As DirectoryInfo = _ New DirectoryInfo(currentDirectory) If Not (directoryObject.Parent Is Nothing) Then LoadFilesInDirectory(directoryObject.Parent.FullName) End If Else Dim chosen As String = lvwBrowser.SelectedItems(0).Text If Directory.Exists(currentDirectory & "\" & chosen) Then If currentDirectory = "C:\" Then LoadFilesInDirectory(currentDirectory & chosen) Else LoadFilesInDirectory(currentDirectory & "\" & chosen) End If End If End If lblDisplay.Text = currentDirectory End If End Sub Public Sub LoadFilesInDirectory(ByVal currentDirectoryValue As String) lvwBrowser.Items.Clear() lvwBrowser.Items.Add("Go Up One Level") currentDirectory = currentDirectoryValue Dim newCurrentDirectory As DirectoryInfo = New DirectoryInfo(currentDirectory) Dim directoryArray As DirectoryInfo() = newCurrentDirectory.GetDirectories() Dim fileArray As FileInfo() = newCurrentDirectory.GetFiles() Dim dir As DirectoryInfo For Each dir In directoryArray Dim newDirectoryItem As ListViewItem = lvwBrowser.Items.Add(dir.Name) Next Dim file As FileInfo For Each file In fileArray Dim newFileItem As ListViewItem =lvwBrowser.Items.Add(file.Name) Next End Sub Private Sub FrmListView_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load LoadFilesInDirectory(currentDirectory) lblDisplay.Text = currentDirectory End Sub
End Class</source>
ListView Demo
<source lang="vbnet">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.ruponentModel.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.ruponents = New System.ruponentModel.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</source>
ListView ItemActivate event
<source lang="vbnet">Imports System.Windows.Forms public class TreeViewListView
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents TreeView1 As System.Windows.Forms.TreeView Friend WithEvents Splitter1 As System.Windows.Forms.Splitter Friend WithEvents ListView1 As System.Windows.Forms.ListView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruponents = New System.ruponentModel.Container() Me.TreeView1 = New System.Windows.Forms.TreeView() Me.Splitter1 = New System.Windows.Forms.Splitter() Me.ListView1 = New System.Windows.Forms.ListView() Me.SuspendLayout() " "TreeView1 " Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left Me.TreeView1.Name = "TreeView1" Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Letter", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("a", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("z"), New System.Windows.Forms.TreeNode("b")}), New System.Windows.Forms.TreeNode("c", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("d"), New System.Windows.Forms.TreeNode("e")})})}) Me.TreeView1.Size = New System.Drawing.Size(121, 273) Me.TreeView1.TabIndex = 0 " "Splitter1 " Me.Splitter1.Location = New System.Drawing.Point(121, 0) Me.Splitter1.Name = "Splitter1" Me.Splitter1.Size = New System.Drawing.Size(3, 273) Me.Splitter1.TabIndex = 1 Me.Splitter1.TabStop = False " "ListView1 " Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill Me.ListView1.Location = New System.Drawing.Point(124, 0) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(168, 273) Me.ListView1.TabIndex = 2 Me.ListView1.View = System.Windows.Forms.View.List " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1, Me.Splitter1, Me.TreeView1}) Me.Name = "Form1" Me.Text = "Vehicle Hierarchy" Me.ResumeLayout(False)
End Sub
- End Region
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect select case e.Node.Text case "a" listView1.Clear() listView1.Items.Add("a",3) listView1.Items.Add("b", 4) listView1.Items.Add("c", 6) case "b" listView1.Clear() listView1.Items.Add("a", 1) listView1.Items.Add("b", 2) listView1.Items.Add("c", 5) end select End Sub Private Sub ListView1_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.ItemActivate dim strItem as String = listView1.FocusedItem.Text Console.WriteLine(strItem) End Sub
End Class</source>
ListView Item Check event
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListViewViewChange
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 0", 0) Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 1", 0) Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 2", 0) Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 3", 0) Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.ListView1 = New System.Windows.Forms.ListView Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.TextBox1 = New System.Windows.Forms.TextBox Me.ruboBox1 = New System.Windows.Forms.ruboBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "ListView1 " Me.ListView1.AllowColumnReorder = True Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3, ListViewItem4}) Me.ListView1.Location = New System.Drawing.Point(32, 104) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(216, 112) Me.ListView1.TabIndex = 1 " "ColumnHeader1 " Me.ColumnHeader1.Text = "Column Header 1" " "ColumnHeader2 " Me.ColumnHeader2.Text = "Column Header 2" " "ColumnHeader3 " Me.ColumnHeader3.Text = "Column Header 3" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(64, 232) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(160, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" " "ComboBox1 " Me.ruboBox1.Items.AddRange(New Object() {"Large Icon View", "Report View", "Small Icon View", "List View"}) Me.ruboBox1.Location = New System.Drawing.Point(16, 64) Me.ruboBox1.Name = "ComboBox1" Me.ruboBox1.Size = New System.Drawing.Size(121, 21) Me.ruboBox1.TabIndex = 3 " "Button1 " Me.Button1.Location = New System.Drawing.Point(144, 64) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(112, 23) Me.Button1.TabIndex = 4 Me.Button1.Text = "Show Check Boxes" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.ruboBox1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListView1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ListView1.View = ComboBox1.SelectedIndex End Sub Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedIndices.Count > 0 Then TextBox1.Text = "Item " & ListView1.SelectedIndices(0) & " was clicked." End If End Sub Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick TextBox1.Text = "Column " & (e.Column + 1) & " was clicked." End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.CheckBoxes = True End Sub Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck If e.NewValue = CheckState.Checked Then TextBox1.Text = "Item " & e.Index() & " is checked." Else TextBox1.Text = "Item " & e.Index() & " is not checked." End If End Sub
End Class</source>
ListView with CheckBox cell
<source lang="vbnet">"Visual Basic.Net JingCai Programming 100 Examples "Author: Yong Zhang "Publisher: Water Publisher China "ISBN: 750841156
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports System.IO public class FileListView
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Dim nIndex As Integer = 0 Private Sub CreateMyListView(ByVal dirpath As String) Dim hImgSmall As IntPtr Dim hImgLarge As IntPtr Dim shinfo As FileInfoClass.SHFILEINFO = New FileInfoClass.SHFILEINFO() ListView1.Clear() ImageListSmall.Images.Clear() ImageListLarge.Images.Clear() nIndex = 0 ListView1.Columns.Add("File Name", 200, HorizontalAlignment.Left) ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Date", 100, HorizontalAlignment.Left) ListView1.Columns.Add("Attribute", 100, HorizontalAlignment.Center) ListView1.LargeImageList = imageListLarge ListView1.SmallImageList = imageListSmall ListView1.View = View.Details ListView1.LabelEdit = True ListView1.AllowColumnReorder = True ListView1.CheckBoxes = True ListView1.FullRowSelect = True ListView1.GridLines = True ListView1.Sorting = SortOrder.Ascending Dim FilesInDir As String() = Directory.GetFiles(dirpath, "*.*") Dim SFile As String Dim item1 As New ListViewItem("", 0) item1.SubItems.Add("1") item1.SubItems.Add("2") item1.SubItems.Add("3") Dim ctdate As Date Dim fAttr As FileAttribute For Each SFile In FilesInDir ctdate = IO.File.GetCreationTime(SFile) fAttr = IO.File.GetAttributes(SFile) shinfo.szDisplayName = New String(Chr(0), 260) shinfo.szTypeName = New String(Chr(0), 80) hImgSmall = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, _ Marshal.SizeOf(shinfo), _ FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_SMALLICON) ImageListSmall.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon)) "Add icon to smallimageList. hImgLarge = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, _ Marshal.SizeOf(shinfo), _ FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_LARGEICON) ImageListLarge.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon)) "Add icon to LargeimageList. ListView1.Items.Add(New ListViewItem(New String() {SFile, CStr(FileLen(SFile)), ctdate.ToString, fAttr.ToString}, nIndex)) nIndex = nIndex + 1 Next End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim disks As String() = Directory.GetLogicalDrives Dim Sdisk As String ComboBox1.Items.Clear() For Each Sdisk In disks ComboBox1.Items.Add(Sdisk) Next End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged CreateMyListView(ComboBox1.Text) End Sub
End Class Public Class FileInfoClass
Public Structure SHFILEINFO Public hIcon As IntPtr Public iIcon As Integer Public dwAttributes As Integer <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _ Public szDisplayName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _ Public szTypeName As String End Structure Public Declare Auto Function SHGetFileInfo Lib "shell32.dll" _ (ByVal pszPath As String, _ ByVal dwFileAttributes As Integer, _ ByRef psfi As SHFILEINFO, _ ByVal cbFileInfo As Integer, _ ByVal uFlags As Integer) As IntPtr Public Const SHGFI_ICON = &H100 Public Const SHGFI_SMALLICON = &H1 Public Const SHGFI_LARGEICON = &H0 " Large icon
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected 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.ruponentModel.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.ruponents = New System.ruponentModel.Container Me.ruboBox1 = New System.Windows.Forms.ruboBox Me.ListView1 = New System.Windows.Forms.ListView Me.ImageListSmall = New System.Windows.Forms.ImageList(Me.ruponents) Me.ImageListLarge = New System.Windows.Forms.ImageList(Me.ruponents) Me.SuspendLayout() " "ComboBox1 " Me.ruboBox1.FormattingEnabled = True Me.ruboBox1.Location = New System.Drawing.Point(12, 3) Me.ruboBox1.Name = "ComboBox1" Me.ruboBox1.Size = New System.Drawing.Size(413, 20) Me.ruboBox1.TabIndex = 0 " "ListView1 " Me.ListView1.Location = New System.Drawing.Point(12, 29) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(413, 207) Me.ListView1.TabIndex = 1 Me.ListView1.UseCompatibleStateImageBehavior = False " "ImageListSmall " Me.ImageListSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit Me.ImageListSmall.ImageSize = New System.Drawing.Size(16, 16) Me.ImageListSmall.TransparentColor = System.Drawing.Color.Transparent " "ImageListLarge " Me.ImageListLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit Me.ImageListLarge.ImageSize = New System.Drawing.Size(16, 16) Me.ImageListLarge.TransparentColor = System.Drawing.Color.Transparent " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(437, 246) Me.Controls.Add(Me.ListView1) Me.Controls.Add(Me.ruboBox1) Me.ResumeLayout(False) End Sub Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents ImageListSmall As System.Windows.Forms.ImageList Friend WithEvents ImageListLarge As System.Windows.Forms.ImageList
End Class</source>
Runtime ListView
<source lang="vbnet">"Visual Basic 2005 Programmer"s Reference "by Rod Stephens (Author) "# Publisher: Wrox (October 21, 2005) "# Language: English "# ISBN-10: 0764571982 "# ISBN-13: 978-0764571985
Imports System.Windows.Forms
public class ListViewRunTime
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load " Make the ListView column headers. ListViewMakeColumnHeaders(lvwBooks, _ "Title", HorizontalAlignment.Left, 120, _ "URL", HorizontalAlignment.Left, 120, _ "ISBN", HorizontalAlignment.Left, 90, _ "Picture", HorizontalAlignment.Left, 120, _ "Pages", HorizontalAlignment.Right, 50, _ "Year", HorizontalAlignment.Right, 40) ListViewMakeRow(lvwBooks, 0, _ "Visual Basic and XML", _ "http://www.vb-helper.ru/xml.htm", _ "0-471-12060-X", _ "http://www.vb-helper.ru/xml.jpg", _ "503", _ "2002") ListViewMakeRow(lvwBooks, 0, _ "Visual Basic Graphics Programming, 2e", _ "http://www.vb-helper.ru/vbgp.htm", _ "0-471-35599-2", _ "http://www.vb-helper.ru/vbgp.jpg", _ "712", _ "2000") ListViewMakeRow(lvwBooks, 0, _ "Ready-to-Run Visual Basic Algorithms", _ "http://www.vb-helper.ru/vba.htm", _ "0-471-24268-3", _ "http://www.vb-helper.ru/vba.jpg", _ "395", _ "1998") ListViewSizeColumns(lvwBooks, True) Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" lvwBooks.View = View.Details CheckMenus() End Sub " Make the ListView"s column headers. " The ParamArray entries should be triples holding " column title, HorizontalAlignment value, and width. Private Sub ListViewMakeColumnHeaders(ByVal lvw As ListView, ByVal ParamArray header_info() As Object) " Remove any existing headers. lvw.Columns.Clear() " Make the column headers. For i As Integer = header_info.GetLowerBound(0) To header_info.GetUpperBound(0) Step 3 Dim col_header As ColumnHeader = lvw.Columns.Add( _ DirectCast(header_info(i), String), _ -1, _ DirectCast(header_info(i + 1), HorizontalAlignment)) col_header.Width = DirectCast(header_info(i + 2), Integer) Next i End Sub " Make a ListView row. Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal image_index As Integer, ByVal item_title As String, ByVal ParamArray subitem_titles() As String) " Make the item. Dim new_item As ListViewItem = lvw.Items.Add(item_title) new_item.ImageIndex = image_index " Make the sub-items. For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0) new_item.SubItems.Add(subitem_titles(i)) Next i End Sub " Set column widths to -1 to fit data, " -2 to fit data and header. Private Sub ListViewSizeColumns(ByVal lvw As ListView, ByVal allow_room_for_header As Boolean) Dim new_wid As Integer = -1 If allow_room_for_header Then new_wid = -2 " Set the width for each column. For i As Integer = 0 To lvw.Columns.Count - 1 lvw.Columns(i).Width = new_wid Next i End Sub Private Sub mnuViewDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewDetails.Click lvwBooks.View = View.Details Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click lvwBooks.View = View.LargeIcon Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click lvwBooks.View = View.List Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click lvwBooks.View = View.SmallIcon Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click lvwBooks.View = View.Tile Text = "RunTimeListView (" & lvwBooks.View.ToString & ")" CheckMenus() End Sub Private Sub CheckMenus() mnuViewDetails.Checked = (lvwBooks.View = View.Details) mnuViewLargeIcons.Checked = (lvwBooks.View = View.LargeIcon) mnuViewList.Checked = (lvwBooks.View = View.List) mnuViewSmallIcons.Checked = (lvwBooks.View = View.SmallIcon) mnuViewTile.Checked = (lvwBooks.View = View.Tile) End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "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.ruponentModel.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.ruponents = New System.ruponentModel.Container Me.MenuStrip1 = New System.Windows.Forms.MenuStrip Me.ViewToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem Me.mnuViewDetails = New System.Windows.Forms.ToolStripMenuItem Me.mnuViewLargeIcons = New System.Windows.Forms.ToolStripMenuItem Me.mnuViewList = New System.Windows.Forms.ToolStripMenuItem Me.mnuViewSmallIcons = New System.Windows.Forms.ToolStripMenuItem Me.mnuViewTile = New System.Windows.Forms.ToolStripMenuItem Me.lvwBooks = New System.Windows.Forms.ListView Me.MenuStrip1.SuspendLayout() Me.SuspendLayout() " "MenuStrip1 " Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ViewToolStripMenuItem}) Me.MenuStrip1.Location = New System.Drawing.Point(0, 0) Me.MenuStrip1.Name = "MenuStrip1" Me.MenuStrip1.Size = New System.Drawing.Size(592, 24) Me.MenuStrip1.TabIndex = 2 Me.MenuStrip1.Text = "MenuStrip1" " "ViewToolStripMenuItem " Me.ViewToolStripMenuItem.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.mnuViewDetails, Me.mnuViewLargeIcons, Me.mnuViewList, Me.mnuViewSmallIcons, Me.mnuViewTile}) Me.ViewToolStripMenuItem.Name = "ViewToolStripMenuItem" Me.ViewToolStripMenuItem.Text = "&View" " "mnuViewDetails " Me.mnuViewDetails.Name = "mnuViewDetails" Me.mnuViewDetails.Text = "&Details" " "mnuViewLargeIcons " Me.mnuViewLargeIcons.Name = "mnuViewLargeIcons" Me.mnuViewLargeIcons.Text = "Large Icons" " "mnuViewList " Me.mnuViewList.Name = "mnuViewList" Me.mnuViewList.Text = "&List" " "mnuViewSmallIcons " Me.mnuViewSmallIcons.Name = "mnuViewSmallIcons" Me.mnuViewSmallIcons.Text = "&Small Icons" " "mnuViewTile " Me.mnuViewTile.Name = "mnuViewTile" Me.mnuViewTile.Text = "Tile" " "lvwBooks " Me.lvwBooks.Dock = System.Windows.Forms.DockStyle.Fill Me.lvwBooks.Location = New System.Drawing.Point(0, 24) Me.lvwBooks.Name = "lvwBooks" Me.lvwBooks.Size = New System.Drawing.Size(592, 249) Me.lvwBooks.TabIndex = 1 Me.lvwBooks.View = System.Windows.Forms.View.Details " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(592, 273) Me.Controls.Add(Me.lvwBooks) Me.Controls.Add(Me.MenuStrip1) Me.Name = "Form1" Me.Text = "RunTimeListView" Me.MenuStrip1.ResumeLayout(False) Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip Friend WithEvents ViewToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem Friend WithEvents mnuViewDetails As System.Windows.Forms.ToolStripMenuItem Friend WithEvents mnuViewLargeIcons As System.Windows.Forms.ToolStripMenuItem Friend WithEvents mnuViewList As System.Windows.Forms.ToolStripMenuItem Friend WithEvents mnuViewSmallIcons As System.Windows.Forms.ToolStripMenuItem Friend WithEvents mnuViewTile As System.Windows.Forms.ToolStripMenuItem Friend WithEvents lvwBooks As System.Windows.Forms.ListView
End Class</source>