VB.Net/GUI/ListView
Содержание
- 1 Add Customized Item to ListView
- 2 Add Items to ListView
- 3 Add Selection Listener to the ListView
- 4 Build ListView at run time: update title column text
- 5 Construct ListView at run time: build structure and insert records
- 6 Displaying directories and their contents in ListView
- 7 Drag and Drop Explorer
- 8 Fill XML data into ListView and add column to ListView at run time
- 9 Large Icon and small Icon for ListView
- 10 ListView Custom sort
- 11 ListView DataReader
- 12 Set ListView header and fill data
Add Customized Item to ListView
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Data Imports System.IO Imports System.Collections Imports System.Windows.Forms Imports System.Drawing.Printing Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(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 lstFavorites As System.Windows.Forms.ListView Friend WithEvents hdrName As System.Windows.Forms.ColumnHeader Friend WithEvents hdrUrl As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstFavorites = New System.Windows.Forms.ListView() Me.hdrName = New System.Windows.Forms.ColumnHeader() Me.hdrUrl = New System.Windows.Forms.ColumnHeader() Me.SuspendLayout() " "lstFavorites " Me.lstFavorites.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.lstFavorites.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.hdrName, Me.hdrUrl}) Me.lstFavorites.Location = New System.Drawing.Point(8, 8) Me.lstFavorites.Name = "lstFavorites" Me.lstFavorites.Size = New System.Drawing.Size(504, 216) Me.lstFavorites.TabIndex = 2 Me.lstFavorites.View = System.Windows.Forms.View.Details " "hdrName " Me.hdrName.Text = "Name" Me.hdrName.Width = 250 " "hdrUrl " Me.hdrUrl.Text = "URL" Me.hdrUrl.Width = 250 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(520, 261) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstFavorites}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) Dim item As New YourListViewItem("First Item") lstFavorites.Items.Add(item) If lstFavorites.Items.Count = 1 Then item.Selected = True End If End Sub
End Class
Public Class YourListViewItem
Inherits ListViewItem Public ItemName As String Public Sub New(ByVal i As String) ItemName = i Text = i SubItems.Add("Sub " & i) End Sub
End Class
</source>
Add Items to ListView
<source lang="vbnet"> Imports System.Windows.Forms Imports System.Drawing Imports System.IO
Module Module1
Sub Main() Application.Run(New Form1) End Sub
End Module
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 Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ListView1 = New System.Windows.Forms.ListView() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() " "ListView1 " Me.ListView1.Location = New System.Drawing.Point(32, 32) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(224, 168) Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.List " "Button1 " Me.Button1.Location = New System.Drawing.Point(96, 224) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 23) Me.Button1.TabIndex = 1 Me.Button1.Text = "Show Files" " "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.Button1, Me.ListView1}) Me.Name = "Form1" Me.Text = "ListViewDemo" Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Files() As String = Directory.GetFiles("C:\") ListView1.View = System.Windows.Forms.View.List Dim Filename As String For Each Filename In Files ListView1.BeginUpdate() ListView1.Items.Add(Filename) ListView1.EndUpdate() ListView1.Refresh() Next End Sub
End Class
</source>
Add Selection Listener to the ListView
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Data Imports System.IO Imports System.Collections Imports System.Windows.Forms Imports System.Drawing.Printing Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(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 lstFavorites As System.Windows.Forms.ListView Friend WithEvents hdrName As System.Windows.Forms.ColumnHeader Friend WithEvents hdrUrl As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstFavorites = New System.Windows.Forms.ListView() Me.hdrName = New System.Windows.Forms.ColumnHeader() Me.hdrUrl = New System.Windows.Forms.ColumnHeader() Me.SuspendLayout() " "lstFavorites " Me.lstFavorites.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.lstFavorites.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.hdrName, Me.hdrUrl}) Me.lstFavorites.Location = New System.Drawing.Point(8, 8) Me.lstFavorites.Name = "lstFavorites" Me.lstFavorites.Size = New System.Drawing.Size(504, 216) Me.lstFavorites.TabIndex = 2 Me.lstFavorites.View = System.Windows.Forms.View.Details " "hdrName " Me.hdrName.Text = "Name" Me.hdrName.Width = 250 " "hdrUrl " Me.hdrUrl.Text = "URL" Me.hdrUrl.Width = 250 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(520, 261) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstFavorites}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) Dim item As New YourListViewItem("First Item") lstFavorites.Items.Add(item) If lstFavorites.Items.Count = 1 Then item.Selected = True End If End Sub
Private Sub lstFavorites_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstFavorites.Click Dim item As YourListViewItem For Each item In lstFavorites.Items If item.Selected = True Then MessageBox.Show(item.ItemName) Exit For End If Next End Sub
End Class
Public Class YourListViewItem
Inherits ListViewItem Public ItemName As String Public Sub New(ByVal i As String) ItemName = i Text = i SubItems.Add("Sub " & i) End Sub
End Class
</source>
Build ListView at run time: update title column text
<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
" The column currently used for sorting. Private m_SortingColumn As ColumnHeader Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load " Make the ListView column headers. ListViewMakeColumnHeaders(lvwStudents, _ "ID", HorizontalAlignment.Left, 120, _ "First Name", HorizontalAlignment.Left, 120, _ "Last Name", HorizontalAlignment.Left, 90, _ "Department", HorizontalAlignment.Left, 120, _ "Start Time", HorizontalAlignment.Right, 50, _ "End Time", HorizontalAlignment.Right, 40) ListViewMakeRow(lvwStudents, 0, _ "01", _ "Joe", _ "Yin", _ "Computer Science", _ "2002", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "02", _ "Chris", _ "Chan", _ "Chemistry", _ "2000", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "03", _ "A", _ "B", _ "Physics", _ "2004", _ "2007") ListViewSizeColumns(lvwStudents, True) Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub ListViewMakeColumnHeaders(ByVal lvw As ListView, ByVal ParamArray header_info() As Object) lvw.Columns.Clear() 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 Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal image_index As Integer, ByVal item_title As String, ByVal ParamArray subitem_titles() As String) Dim new_item As ListViewItem = lvw.Items.Add(item_title) new_item.ImageIndex = image_index For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0) new_item.SubItems.Add(subitem_titles(i)) Next i End Sub 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 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 lvwStudents.View = View.Details Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click lvwStudents.View = View.LargeIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click lvwStudents.View = View.List Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click lvwStudents.View = View.SmallIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click lvwStudents.View = View.Tile Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub lvwStudents_ColumnClick(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwStudents.ColumnClick Dim new_sorting_column As ColumnHeader = _ lvwStudents.Columns(e.Column) Dim sort_order As System.Windows.Forms.SortOrder If m_SortingColumn Is Nothing Then sort_order = SortOrder.Ascending Else If new_sorting_column.Equals(m_SortingColumn) Then If m_SortingColumn.Text.StartsWith("> ") Then sort_order = SortOrder.Descending Else sort_order = SortOrder.Ascending End If Else sort_order = SortOrder.Ascending End If m_SortingColumn.Text = m_SortingColumn.Text.Substring(2) End If m_SortingColumn = new_sorting_column If sort_order = SortOrder.Ascending Then m_SortingColumn.Text = "> " & m_SortingColumn.Text Else m_SortingColumn.Text = "< " & m_SortingColumn.Text End If lvwStudents.ListViewItemSorter = New ListViewComparer(e.Column, sort_order) lvwStudents.Sort() End Sub
End Class Public Class ListViewComparer
Implements IComparer Private m_ColumnNumber As Integer Private m_SortOrder As SortOrder Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder) m_ColumnNumber = column_number m_SortOrder = sort_order End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) _ As Integer Implements System.Collections.IComparer.rupare Dim item_x As ListViewItem = DirectCast(x, ListViewItem) Dim item_y As ListViewItem = DirectCast(y, ListViewItem) " Get the sub-item values. Dim string_x As String If item_x.SubItems.Count <= m_ColumnNumber Then string_x = "" Else string_x = item_x.SubItems(m_ColumnNumber).Text End If Dim string_y As String If item_y.SubItems.Count <= m_ColumnNumber Then string_y = "" Else string_y = item_y.SubItems(m_ColumnNumber).Text End If " Compare them. If m_SortOrder = SortOrder.Ascending Then Return String.rupare(string_x, string_y) Else Return String.rupare(string_y, string_x) End If End Function
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 "Dim resources As System.ruponentModel.ruponentResourceManager = New System.ruponentModel.ruponentResourceManager(GetType(Form1)) "Me.imlLarge = New System.Windows.Forms.ImageList(Me.ruponents) 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.imlSmall = New System.Windows.Forms.ImageList(Me.ruponents) Me.lvwStudents = New System.Windows.Forms.ListView Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.MenuStrip1.SuspendLayout() Me.SuspendLayout() " "imlLarge " "Me.imlLarge.ImageStream = CType(resources.GetObject("imlLarge.ImageStream"), System.Windows.Forms.ImageListStreamer) "Me.imlLarge.Images.SetKeyName(0, "large_book.bmp") " "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(772, 24) Me.MenuStrip1.TabIndex = 1 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" " "imlSmall " "Me.imlSmall.ImageStream = CType(resources.GetObject("imlSmall.ImageStream"), System.Windows.Forms.ImageListStreamer) "Me.imlSmall.Images.SetKeyName(0, "small_book.bmp") " "lvwStudents " Me.lvwStudents.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.lvwStudents.Dock = System.Windows.Forms.DockStyle.Fill "Me.lvwStudents.LargeImageList = Me.imlLarge Me.lvwStudents.Location = New System.Drawing.Point(0, 24) Me.lvwStudents.Name = "lvwStudents" Me.lvwStudents.Size = New System.Drawing.Size(772, 249) "Me.lvwStudents.SmallImageList = Me.imlSmall Me.lvwStudents.TabIndex = 2 Me.lvwStudents.View = System.Windows.Forms.View.Details " "ColumnHeader2 " Me.ColumnHeader2.Text = "URL" " "ColumnHeader1 " Me.ColumnHeader1.Text = "Title" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(772, 273) Me.Controls.Add(Me.lvwStudents) Me.Controls.Add(Me.MenuStrip1) Me.Name = "Form1" Me.Text = "ListViewCustomSort" Me.MenuStrip1.ResumeLayout(False) Me.ResumeLayout(False) Me.PerformLayout() End Sub
" Friend WithEvents imlLarge As System.Windows.Forms.ImageList
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 imlSmall As System.Windows.Forms.ImageList Friend WithEvents lvwStudents As System.Windows.Forms.ListView Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
End Class
</source>
Construct ListView at run time: build structure and insert records
<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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(lvwStudents, _ "ID", HorizontalAlignment.Left, 120, _ "First Name", HorizontalAlignment.Left, 120, _ "Last Name", HorizontalAlignment.Left, 90, _ "Department", HorizontalAlignment.Left, 120, _ "Start Time", HorizontalAlignment.Right, 50, _ "End Time", HorizontalAlignment.Right, 40) ListViewMakeRow(lvwStudents, 0, _ "01", _ "Joe", _ "Yin", _ "Computer Science", _ "2002", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "02", _ "Chris", _ "Chan", _ "Chemistry", _ "2000", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "03", _ "A", _ "B", _ "Physics", _ "2004", _ "2007") ListViewSizeColumns(lvwStudents, True) Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" lvwStudents.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 lvwStudents.View = View.Details Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click lvwStudents.View = View.LargeIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click lvwStudents.View = View.List Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click lvwStudents.View = View.SmallIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" CheckMenus() End Sub Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click lvwStudents.View = View.Tile Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" CheckMenus() End Sub Private Sub CheckMenus() mnuViewDetails.Checked = (lvwStudents.View = View.Details) mnuViewLargeIcons.Checked = (lvwStudents.View = View.LargeIcon) mnuViewList.Checked = (lvwStudents.View = View.List) mnuViewSmallIcons.Checked = (lvwStudents.View = View.SmallIcon) mnuViewTile.Checked = (lvwStudents.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 "Dim resources As System.ruponentModel.ruponentResourceManager = New System.ruponentModel.ruponentResourceManager(GetType(Form1)) 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.lvwStudents = New System.Windows.Forms.ListView "Me.imlSmall = New System.Windows.Forms.ImageList(Me.ruponents) "Me.imlLarge = New System.Windows.Forms.ImageList(Me.ruponents) 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" " "lvwStudents " Me.lvwStudents.Dock = System.Windows.Forms.DockStyle.Fill "Me.lvwStudents.LargeImageList = Me.imlLarge Me.lvwStudents.Location = New System.Drawing.Point(0, 24) Me.lvwStudents.Name = "lvwStudents" Me.lvwStudents.Size = New System.Drawing.Size(592, 249) "Me.lvwStudents.SmallImageList = Me.imlSmall Me.lvwStudents.TabIndex = 1 Me.lvwStudents.View = System.Windows.Forms.View.Details " "imlSmall " "Me.imlSmall.ImageStream = CType(resources.GetObject("imlSmall.ImageStream"), System.Windows.Forms.ImageListStreamer) "Me.imlSmall.Images.SetKeyName(0, "small_book.bmp") " "imlLarge " " Me.imlLarge.ImageStream = CType(resources.GetObject("imlLarge.ImageStream"), System.Windows.Forms.ImageListStreamer) " Me.imlLarge.Images.SetKeyName(0, "large_book.bmp") " "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.lvwStudents) 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 lvwStudents As System.Windows.Forms.ListView "Friend WithEvents imlSmall As System.Windows.Forms.ImageList "Friend WithEvents imlLarge As System.Windows.Forms.ImageList
End Class
</source>
Displaying directories and their contents in ListView
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.IO Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmListView() Application.Run(myform) End Sub " Main
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) Try 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) newDirectoryItem.ImageIndex = 0 Next Dim file As FileInfo For Each file In fileArray Dim newFileItem As ListViewItem = _ lvwBrowser.Items.Add(file.Name) newFileItem.ImageIndex = 1 " set file image Next Catch exception As UnauthorizedAccessException MessageBox.Show("Warning: Some files may " & _ "not be visible due to " + "permission settings", _ "Attention", 0, MessageBoxIcon.Warning) End Try End Sub " LoadFilesInDirectory " handle load event when Form displayed for first time Private Sub FrmListView_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load " set image list Dim folderImage As Image = Image.FromFile("figure2.bmp") Dim fileImage As Image = Image.FromFile("figure2.bmp") ilsFileFolder.Images.Add(folderImage) ilsFileFolder.Images.Add(fileImage) " load current directory into browserListView LoadFilesInDirectory(currentDirectory) lblDisplay.Text = currentDirectory End Sub " FrmListView_Load
End Class
</source>
Drag and Drop Explorer
<source lang="vbnet"> Imports System.Windows.Forms Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents ImageList1 As System.Windows.Forms.ImageList Friend WithEvents Filename As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.ListView1 = New System.Windows.Forms.ListView() Me.Label1 = New System.Windows.Forms.Label() Me.ImageList1 = New System.Windows.Forms.ImageList() Me.Filename = New System.Windows.Forms.ColumnHeader() Me.SuspendLayout() " "ListView1 " Me.ListView1.AllowDrop = True Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.Filename}) Me.ListView1.Location = New System.Drawing.Point(16, 32) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(368, 296) Me.ListView1.SmallImageList = Me.ImageList1 Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.Details " Me.Label1.Location = New System.Drawing.Point(16, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(360, 16) Me.Label1.TabIndex = 1 Me.Label1.Text = "Drag and drop files from Explorer onto this ListView control:" " Me.ImageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16) Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer) Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent " Me.Filename.Text = "File / folder" Me.Filename.Width = 300 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(400, 342) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.ListView1}) Me.Name = "Form1" Me.Text = "Drag and Drop from Windows Explorer" Me.ResumeLayout(False) End Sub Private Sub ListView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragOver If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then Dim strFiles() As String = e.Data.GetData(DataFormats.FileDrop) Dim intCount As Integer For intCount = 0 To strFiles.Length ListView1.Items.Add(strFiles(intCount), 0) Next End If End Sub
End Class
</source>
Fill XML data into ListView and add column to ListView at run time
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 Friend WithEvents cmdFillList As System.Windows.Forms.Button Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents optSmallIcon As System.Windows.Forms.RadioButton Friend WithEvents optLargeIcon As System.Windows.Forms.RadioButton Friend WithEvents optDetails As System.Windows.Forms.RadioButton Friend WithEvents optList As System.Windows.Forms.RadioButton Friend WithEvents listAuthors As System.Windows.Forms.ListView Private components As System.ruponentModel.IContainer "Required by the Windows Form Designer "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.GroupBox1 = New System.Windows.Forms.GroupBox() Me.optLargeIcon = New System.Windows.Forms.RadioButton() Me.optList = New System.Windows.Forms.RadioButton() Me.optDetails = New System.Windows.Forms.RadioButton() Me.optSmallIcon = New System.Windows.Forms.RadioButton() Me.listAuthors = New System.Windows.Forms.ListView() Me.cmdFillList = New System.Windows.Forms.Button() Me.GroupBox1.SuspendLayout() Me.SuspendLayout() " "GroupBox1 " Me.GroupBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.optLargeIcon, Me.optList, Me.optDetails, Me.optSmallIcon}) Me.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System Me.GroupBox1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.GroupBox1.Location = New System.Drawing.Point(276, 12) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(104, 132) Me.GroupBox1.TabIndex = 2 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "View" " "optLargeIcon " Me.optLargeIcon.Checked = True Me.optLargeIcon.FlatStyle = System.Windows.Forms.FlatStyle.System Me.optLargeIcon.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.optLargeIcon.Location = New System.Drawing.Point(16, 48) Me.optLargeIcon.Name = "optLargeIcon" Me.optLargeIcon.Size = New System.Drawing.Size(76, 16) Me.optLargeIcon.TabIndex = 0 Me.optLargeIcon.TabStop = True Me.optLargeIcon.Text = "LargeIcon" " "optList " Me.optList.FlatStyle = System.Windows.Forms.FlatStyle.System Me.optList.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.optList.Location = New System.Drawing.Point(16, 96) Me.optList.Name = "optList" Me.optList.Size = New System.Drawing.Size(56, 16) Me.optList.TabIndex = 0 Me.optList.Text = "List" " "optDetails " Me.optDetails.FlatStyle = System.Windows.Forms.FlatStyle.System Me.optDetails.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.optDetails.Location = New System.Drawing.Point(16, 72) Me.optDetails.Name = "optDetails" Me.optDetails.Size = New System.Drawing.Size(72, 16) Me.optDetails.TabIndex = 0 Me.optDetails.Text = "Details" " "optSmallIcon " Me.optSmallIcon.FlatStyle = System.Windows.Forms.FlatStyle.System Me.optSmallIcon.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.optSmallIcon.Location = New System.Drawing.Point(16, 24) Me.optSmallIcon.Name = "optSmallIcon" Me.optSmallIcon.Size = New System.Drawing.Size(72, 16) Me.optSmallIcon.TabIndex = 0 Me.optSmallIcon.Text = "SmallIcon" " "listAuthors " Me.listAuthors.Activation = System.Windows.Forms.ItemActivation.OneClick Me.listAuthors.AllowColumnReorder = True Me.listAuthors.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.listAuthors.GridLines = True Me.listAuthors.HoverSelection = True Me.listAuthors.Location = New System.Drawing.Point(8, 8) Me.listAuthors.Name = "listAuthors" Me.listAuthors.Size = New System.Drawing.Size(260, 332) Me.listAuthors.Sorting = System.Windows.Forms.SortOrder.Ascending Me.listAuthors.TabIndex = 0 " "cmdFillList " Me.cmdFillList.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right) Me.cmdFillList.FlatStyle = System.Windows.Forms.FlatStyle.System Me.cmdFillList.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdFillList.Location = New System.Drawing.Point(276, 152) Me.cmdFillList.Name = "cmdFillList" Me.cmdFillList.Size = New System.Drawing.Size(104, 24) Me.cmdFillList.TabIndex = 1 Me.cmdFillList.Text = "Fill List" " "ListViewExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(388, 349) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.cmdFillList, Me.listAuthors}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "ListViewExample" Me.Text = "ListView Example" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private Sub cmdFillList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFillList.Click listAuthors.BeginUpdate() Dim dt As DataTable = StoreDB.GetProducts() Dim dr As DataRow For Each dr In dt.Rows Dim listItem As New ListViewItem(dr("book").ToString) listItem.ImageIndex = 0 listItem.SubItems.Add(dr("title")) listAuthors.Items.Add(listItem) Next listAuthors.Columns.Add("title", 100, HorizontalAlignment.Left) listAuthors.EndUpdate() End Sub Private Sub NewView(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles optSmallIcon.CheckedChanged, optDetails.CheckedChanged, _ optLargeIcon.CheckedChanged, optList.CheckedChanged listAuthors.View = CType(sender, Control).Tag Me.Text = "Using View: " & listAuthors.View.ToString End Sub
Private Sub ListViewExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load optLargeIcon.Tag = View.LargeIcon optSmallIcon.Tag = View.SmallIcon optDetails.Tag = View.Details optList.Tag = View.List Call cmdFillList_Click(Nothing, Nothing) End Sub
Private Sub listAuthors_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles listAuthors.ColumnClick listAuthors.ListViewItemSorter = New CompareListViewItems(e.Column) listAuthors.Sort() End Sub
End Class Public Class CompareListViewItems
Implements IComparer Public ReadOnly Column As Integer Public Sub New(ByVal columnIndex As Integer) Column = columnIndex End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.rupare Dim ListX As ListViewItem = CType(x, ListViewItem) Dim ListY As ListViewItem = CType(y, ListViewItem) If ListX.SubItems(Column).Text > ListY.SubItems(Column).Text Then Return 1 ElseIf ListX.SubItems(Column).Text = ListY.SubItems(Column).Text Then Return 0 Else Return -1 End If End Function
End Class Public Class StoreDB
Public Shared Function GetProducts() As DataTable Dim dsStore As New DataSet() dsStore.ReadXmlSchema("book.xdr") dsStore.ReadXml("book.xml") Return dsStore.Tables("books") End Function
End Class
</source>
Large Icon and small Icon for ListView
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Diagnostics Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) 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 Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Dim resources As System.ruponentModel.ruponentResourceManager = New System.ruponentModel.ruponentResourceManager(GetType(Form1)) Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem5 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem6 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Dim ListViewItem7 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem8 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem9 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Dim ListViewItem10 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem11 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem12 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Dim ListViewItem13 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem14 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem15 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Dim ListViewItem16 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VB/XML", "2002", "503", "No"}, 0) Dim ListViewItem17 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBGP, 2e", "2000", "712", "Yes"}, 0) Dim ListViewItem18 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"VBA, 2e", "1998", "395", "Yes"}, 0) Me.Label6 = New System.Windows.Forms.Label Me.ListView1 = New System.Windows.Forms.ListView Me.hdrTitle = New System.Windows.Forms.ColumnHeader Me.hdrYear = New System.Windows.Forms.ColumnHeader Me.hdrPages = New System.Windows.Forms.ColumnHeader Me.hdrCd = New System.Windows.Forms.ColumnHeader Me.imlBig = New System.Windows.Forms.ImageList(Me.ruponents) Me.imlSmall = New System.Windows.Forms.ImageList(Me.ruponents) Me.Label5 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label1 = New System.Windows.Forms.Label Me.imlChecks = New System.Windows.Forms.ImageList(Me.ruponents) Me.ListView2 = 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.ColumnHeader4 = New System.Windows.Forms.ColumnHeader Me.ListView3 = New System.Windows.Forms.ListView Me.ColumnHeader5 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader6 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader7 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader8 = New System.Windows.Forms.ColumnHeader Me.ListView6 = New System.Windows.Forms.ListView Me.ListView4 = New System.Windows.Forms.ListView Me.ColumnHeader9 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader10 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader11 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader12 = New System.Windows.Forms.ColumnHeader Me.ListView5 = New System.Windows.Forms.ListView Me.SuspendLayout() " "Label6 " Me.Label6.Location = New System.Drawing.Point(496, 184) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(240, 16) Me.Label6.TabIndex = 27 Me.Label6.Text = "Tile" Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "ListView1 " Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.hdrTitle, Me.hdrYear, Me.hdrPages, Me.hdrCd}) Me.ListView1.FullRowSelect = True Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3}) Me.ListView1.LargeImageList = Me.imlBig Me.ListView1.Location = New System.Drawing.Point(0, 16) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(240, 168) Me.ListView1.SmallImageList = Me.imlSmall Me.ListView1.TabIndex = 26 Me.ListView1.View = System.Windows.Forms.View.Details " "hdrTitle " Me.hdrTitle.Text = "Title" Me.hdrTitle.Width = 100 " "hdrYear " Me.hdrYear.Text = "Year" Me.hdrYear.Width = 40 " "hdrPages " Me.hdrPages.Text = "Pages" Me.hdrPages.Width = 50 " "hdrCd " Me.hdrCd.Text = "CD?" Me.hdrCd.Width = 40 " "imlBig " Me.imlBig.ImageStream = CType(resources.GetObject("imlBig.ImageStream"), System.Windows.Forms.ImageListStreamer) Me.imlBig.Images.SetKeyName(0, "large_book.bmp") " "imlSmall " Me.imlSmall.ImageStream = CType(resources.GetObject("imlSmall.ImageStream"), System.Windows.Forms.ImageListStreamer) Me.imlSmall.Images.SetKeyName(0, "small_book.bmp") " "Label5 " Me.Label5.Location = New System.Drawing.Point(248, 184) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(240, 16) Me.Label5.TabIndex = 22 Me.Label5.Text = "Tile" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label3 " Me.Label3.Location = New System.Drawing.Point(496, 0) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(240, 16) Me.Label3.TabIndex = 21 Me.Label3.Text = "List" Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label4 " Me.Label4.Location = New System.Drawing.Point(0, 0) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(240, 16) Me.Label4.TabIndex = 20 Me.Label4.Text = "Details" Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label2 " Me.Label2.Location = New System.Drawing.Point(0, 184) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(240, 16) Me.Label2.TabIndex = 19 Me.Label2.Text = "SmallIcon" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "Label1 " Me.Label1.Location = New System.Drawing.Point(248, 0) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(240, 16) Me.Label1.TabIndex = 18 Me.Label1.Text = "LargeIcon" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter " "imlChecks " Me.imlChecks.ImageStream = CType(resources.GetObject("imlChecks.ImageStream"), System.Windows.Forms.ImageListStreamer) Me.imlChecks.Images.SetKeyName(0, "Copy of 0.bmp") Me.imlChecks.Images.SetKeyName(1, "Copy of 1.bmp") Me.imlChecks.Images.SetKeyName(2, "Copy of 2.bmp") Me.imlChecks.Images.SetKeyName(3, "Copy of 3.bmp") " "ListView2 " Me.ListView2.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4}) Me.ListView2.FullRowSelect = True Me.ListView2.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem4, ListViewItem5, ListViewItem6}) Me.ListView2.LargeImageList = Me.imlBig Me.ListView2.Location = New System.Drawing.Point(248, 16) Me.ListView2.Name = "ListView2" Me.ListView2.Size = New System.Drawing.Size(240, 168) Me.ListView2.SmallImageList = Me.imlSmall Me.ListView2.TabIndex = 28 " "ColumnHeader1 " Me.ColumnHeader1.Text = "Title" Me.ColumnHeader1.Width = 100 " "ColumnHeader2 " Me.ColumnHeader2.Text = "Year" Me.ColumnHeader2.Width = 40 " "ColumnHeader3 " Me.ColumnHeader3.Text = "Pages" Me.ColumnHeader3.Width = 50 " "ColumnHeader4 " Me.ColumnHeader4.Text = "CD?" Me.ColumnHeader4.Width = 40 " "ListView3 " Me.ListView3.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader5, Me.ColumnHeader6, Me.ColumnHeader7, Me.ColumnHeader8}) Me.ListView3.FullRowSelect = True Me.ListView3.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem7, ListViewItem8, ListViewItem9}) Me.ListView3.LargeImageList = Me.imlBig Me.ListView3.Location = New System.Drawing.Point(496, 16) Me.ListView3.Name = "ListView3" Me.ListView3.Size = New System.Drawing.Size(240, 168) Me.ListView3.SmallImageList = Me.imlSmall Me.ListView3.TabIndex = 29 Me.ListView3.View = System.Windows.Forms.View.List " "ColumnHeader5 " Me.ColumnHeader5.Text = "Title" Me.ColumnHeader5.Width = 100 " "ColumnHeader6 " Me.ColumnHeader6.Text = "Year" Me.ColumnHeader6.Width = 40 " "ColumnHeader7 " Me.ColumnHeader7.Text = "Pages" Me.ColumnHeader7.Width = 50 " "ColumnHeader8 " Me.ColumnHeader8.Text = "CD?" Me.ColumnHeader8.Width = 40 " "ListView6 " ListViewItem10.StateImageIndex = 0 ListViewItem11.Checked = True ListViewItem11.StateImageIndex = 2 ListViewItem12.Checked = True ListViewItem12.StateImageIndex = 3 Me.ListView6.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem10, ListViewItem11, ListViewItem12}) Me.ListView6.LargeImageList = Me.imlBig Me.ListView6.Location = New System.Drawing.Point(496, 200) Me.ListView6.Name = "ListView6" Me.ListView6.Size = New System.Drawing.Size(240, 168) Me.ListView6.SmallImageList = Me.imlSmall Me.ListView6.StateImageList = Me.imlChecks Me.ListView6.TabIndex = 33 Me.ListView6.View = System.Windows.Forms.View.Tile " "ListView4 " Me.ListView4.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader9, Me.ColumnHeader10, Me.ColumnHeader11, Me.ColumnHeader12}) Me.ListView4.FullRowSelect = True Me.ListView4.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem13, ListViewItem14, ListViewItem15}) Me.ListView4.LargeImageList = Me.imlBig Me.ListView4.Location = New System.Drawing.Point(0, 200) Me.ListView4.Name = "ListView4" Me.ListView4.Size = New System.Drawing.Size(240, 168) Me.ListView4.SmallImageList = Me.imlSmall Me.ListView4.TabIndex = 34 Me.ListView4.View = System.Windows.Forms.View.SmallIcon " "ColumnHeader9 " Me.ColumnHeader9.Text = "Title" Me.ColumnHeader9.Width = 100 " "ColumnHeader10 " Me.ColumnHeader10.Text = "Year" Me.ColumnHeader10.Width = 40 " "ColumnHeader11 " Me.ColumnHeader11.Text = "Pages" Me.ColumnHeader11.Width = 50 " "ColumnHeader12 " Me.ColumnHeader12.Text = "CD?" Me.ColumnHeader12.Width = 40 " "ListView5 " ListViewItem16.StateImageIndex = 0 ListViewItem17.Checked = True ListViewItem17.StateImageIndex = 2 ListViewItem18.Checked = True ListViewItem18.StateImageIndex = 3 Me.ListView5.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem16, ListViewItem17, ListViewItem18}) Me.ListView5.LargeImageList = Me.imlBig Me.ListView5.Location = New System.Drawing.Point(248, 200) Me.ListView5.Name = "ListView5" Me.ListView5.Size = New System.Drawing.Size(240, 168) Me.ListView5.SmallImageList = Me.imlSmall Me.ListView5.TabIndex = 35 Me.ListView5.View = System.Windows.Forms.View.Tile " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(736, 369) Me.Controls.Add(Me.ListView5) Me.Controls.Add(Me.ListView4) Me.Controls.Add(Me.ListView6) Me.Controls.Add(Me.ListView3) Me.Controls.Add(Me.ListView2) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.ListView1) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "UseListView" Me.ResumeLayout(False) End Sub Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents hdrTitle As System.Windows.Forms.ColumnHeader Friend WithEvents hdrYear As System.Windows.Forms.ColumnHeader Friend WithEvents hdrPages As System.Windows.Forms.ColumnHeader Friend WithEvents hdrCd As System.Windows.Forms.ColumnHeader Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents imlBig As System.Windows.Forms.ImageList Friend WithEvents imlSmall As System.Windows.Forms.ImageList Friend WithEvents imlChecks As System.Windows.Forms.ImageList Friend WithEvents ListView2 As System.Windows.Forms.ListView 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 ColumnHeader4 As System.Windows.Forms.ColumnHeader Friend WithEvents ListView3 As System.Windows.Forms.ListView Friend WithEvents ColumnHeader5 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader6 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader7 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader8 As System.Windows.Forms.ColumnHeader Friend WithEvents ListView6 As System.Windows.Forms.ListView Friend WithEvents ListView4 As System.Windows.Forms.ListView Friend WithEvents ColumnHeader9 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader10 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader11 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader12 As System.Windows.Forms.ColumnHeader Friend WithEvents ListView5 As System.Windows.Forms.ListView
End Class
</source>
ListView Custom sort
<source lang="vbnet"> Imports System Imports System.Data Imports System.Collections Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
" The column currently used for sorting. Private m_SortingColumn As ColumnHeader Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load " Make the ListView column headers. ListViewMakeColumnHeaders(lvwStudents, _ "ID", HorizontalAlignment.Left, 120, _ "First Name", HorizontalAlignment.Left, 120, _ "Last Name", HorizontalAlignment.Left, 90, _ "Department", HorizontalAlignment.Left, 120, _ "Start Time", HorizontalAlignment.Right, 50, _ "End Time", HorizontalAlignment.Right, 40) ListViewMakeRow(lvwStudents, 0, _ "01", _ "Joe", _ "Yin", _ "Computer Science", _ "2002", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "02", _ "Chris", _ "Chan", _ "Chemistry", _ "2000", _ "2004") ListViewMakeRow(lvwStudents, 0, _ "03", _ "A", _ "B", _ "Physics", _ "2004", _ "2007") ListViewSizeColumns(lvwStudents, True) Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub ListViewMakeColumnHeaders(ByVal lvw As ListView, ByVal ParamArray header_info() As Object) lvw.Columns.Clear() 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 Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal image_index As Integer, ByVal item_title As String, ByVal ParamArray subitem_titles() As String) Dim new_item As ListViewItem = lvw.Items.Add(item_title) new_item.ImageIndex = image_index For i As Integer = subitem_titles.GetLowerBound(0) To subitem_titles.GetUpperBound(0) new_item.SubItems.Add(subitem_titles(i)) Next i End Sub 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 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 lvwStudents.View = View.Details Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click lvwStudents.View = View.LargeIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click lvwStudents.View = View.List Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click lvwStudents.View = View.SmallIcon Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click lvwStudents.View = View.Tile Text = "RunTimeListView (" & lvwStudents.View.ToString & ")" End Sub Private Sub lvwStudents_ColumnClick(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwStudents.ColumnClick Dim new_sorting_column As ColumnHeader = _ lvwStudents.Columns(e.Column) Dim sort_order As System.Windows.Forms.SortOrder If m_SortingColumn Is Nothing Then sort_order = SortOrder.Ascending Else If new_sorting_column.Equals(m_SortingColumn) Then If m_SortingColumn.Text.StartsWith("> ") Then sort_order = SortOrder.Descending Else sort_order = SortOrder.Ascending End If Else sort_order = SortOrder.Ascending End If m_SortingColumn.Text = m_SortingColumn.Text.Substring(2) End If m_SortingColumn = new_sorting_column If sort_order = SortOrder.Ascending Then m_SortingColumn.Text = "> " & m_SortingColumn.Text Else m_SortingColumn.Text = "< " & m_SortingColumn.Text End If lvwStudents.ListViewItemSorter = New ListViewComparer(e.Column, sort_order) lvwStudents.Sort() End Sub
End Class Public Class ListViewComparer
Implements IComparer Private m_ColumnNumber As Integer Private m_SortOrder As SortOrder Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder) m_ColumnNumber = column_number m_SortOrder = sort_order End Sub Public Function Compare(ByVal x As Object, ByVal y As Object) _ As Integer Implements System.Collections.IComparer.rupare Dim item_x As ListViewItem = DirectCast(x, ListViewItem) Dim item_y As ListViewItem = DirectCast(y, ListViewItem) " Get the sub-item values. Dim string_x As String If item_x.SubItems.Count <= m_ColumnNumber Then string_x = "" Else string_x = item_x.SubItems(m_ColumnNumber).Text End If Dim string_y As String If item_y.SubItems.Count <= m_ColumnNumber Then string_y = "" Else string_y = item_y.SubItems(m_ColumnNumber).Text End If " Compare them. If m_SortOrder = SortOrder.Ascending Then Return String.rupare(string_x, string_y) Else Return String.rupare(string_y, string_x) End If End Function
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 "Dim resources As System.ruponentModel.ruponentResourceManager = New System.ruponentModel.ruponentResourceManager(GetType(Form1)) "Me.imlLarge = New System.Windows.Forms.ImageList(Me.ruponents) 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.imlSmall = New System.Windows.Forms.ImageList(Me.ruponents) Me.lvwStudents = New System.Windows.Forms.ListView Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.MenuStrip1.SuspendLayout() Me.SuspendLayout() " "imlLarge " "Me.imlLarge.ImageStream = CType(resources.GetObject("imlLarge.ImageStream"), System.Windows.Forms.ImageListStreamer) "Me.imlLarge.Images.SetKeyName(0, "large_book.bmp") " "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(772, 24) Me.MenuStrip1.TabIndex = 1 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" " "imlSmall " "Me.imlSmall.ImageStream = CType(resources.GetObject("imlSmall.ImageStream"), System.Windows.Forms.ImageListStreamer) "Me.imlSmall.Images.SetKeyName(0, "small_book.bmp") " "lvwStudents " Me.lvwStudents.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.lvwStudents.Dock = System.Windows.Forms.DockStyle.Fill "Me.lvwStudents.LargeImageList = Me.imlLarge Me.lvwStudents.Location = New System.Drawing.Point(0, 24) Me.lvwStudents.Name = "lvwStudents" Me.lvwStudents.Size = New System.Drawing.Size(772, 249) "Me.lvwStudents.SmallImageList = Me.imlSmall Me.lvwStudents.TabIndex = 2 Me.lvwStudents.View = System.Windows.Forms.View.Details " "ColumnHeader2 " Me.ColumnHeader2.Text = "URL" " "ColumnHeader1 " Me.ColumnHeader1.Text = "Title" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(772, 273) Me.Controls.Add(Me.lvwStudents) Me.Controls.Add(Me.MenuStrip1) Me.Name = "Form1" Me.Text = "ListViewCustomSort" Me.MenuStrip1.ResumeLayout(False) Me.ResumeLayout(False) Me.PerformLayout() End Sub
" Friend WithEvents imlLarge As System.Windows.Forms.ImageList
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 imlSmall As System.Windows.Forms.ImageList Friend WithEvents lvwStudents As System.Windows.Forms.ListView Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
End Class
</source>
ListView DataReader
<source lang="vbnet"> Imports System.Data.SqlClient Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class List
Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.cmdClear = New System.Windows.Forms.Button Me.cmdFill = New System.Windows.Forms.Button Me.lvOrders = New System.Windows.Forms.ListView Me.SuspendLayout() " Me.cmdClear.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.cmdClear.Location = New System.Drawing.Point(206, 231) Me.cmdClear.Size = New System.Drawing.Size(80, 24) Me.cmdClear.TabIndex = 5 Me.cmdClear.Text = "Clear List" " Me.cmdFill.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.cmdFill.Location = New System.Drawing.Point(10, 231) Me.cmdFill.Size = New System.Drawing.Size(80, 24) Me.cmdFill.TabIndex = 4 Me.cmdFill.Text = "Fill List" " Me.lvOrders.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lvOrders.Location = New System.Drawing.Point(6, 11) Me.lvOrders.Size = New System.Drawing.Size(280, 212) Me.lvOrders.TabIndex = 3 Me.lvOrders.UseCompatibleStateImageBehavior = False Me.lvOrders.View = System.Windows.Forms.View.Details " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.cmdClear) Me.Controls.Add(Me.cmdFill) Me.Controls.Add(Me.lvOrders) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.ResumeLayout(False) End Sub Friend WithEvents cmdClear As System.Windows.Forms.Button Friend WithEvents cmdFill As System.Windows.Forms.Button Friend WithEvents lvOrders As System.Windows.Forms.ListView Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click lvOrders.Clear() End Sub Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind" Dim con As New SqlConnection(Connect) Dim SQLString As String = "SELECT * FROM Orders" Dim cmd As New SqlCommand(SQLString, con) con.Open() Dim reader As SqlDataReader = cmd.ExecuteReader() Dim i As Integer For i = 0 To reader.FieldCount - 1 lvOrders.Columns.Add("Column " & (i + 1).ToString, 100, _ HorizontalAlignment.Left) Next Do While (reader.Read()) Dim NewItem As New ListViewItem() NewItem.Text = reader(0) For i = 1 To reader.FieldCount - 1 If reader(i) Is DBNull.Value Then NewItem.SubItems.Add("") Else NewItem.SubItems.Add(reader(i).ToString) End If Next i lvOrders.Items.Add(NewItem) Loop Dim Table As DataTable = reader.GetSchemaTable() For j As Integer = 0 To Table.Rows.Count - 1 lvOrders.Columns(j).Text = Table.Rows(j)("ColumnName") Next reader.Close() con.Close() End Sub
End Class
</source>
Set ListView header and fill data
<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Public Class MainClass
Shared Sub Main() Dim myform As Form = New Form1() Application.Run(myform) 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() Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New String() {"A", "B", "J", "C", "D"}, -1) Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("E") Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("F") 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.ColumnHeader4 = New System.Windows.Forms.ColumnHeader Me.SuspendLayout() " "ListView1 " Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4}) Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3}) Me.ListView1.Location = New System.Drawing.Point(8, 16) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(360, 104) Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.Details " "ColumnHeader1 " Me.ColumnHeader1.Text = "Item1" " "ColumnHeader2 " Me.ColumnHeader2.Text = "Item2" Me.ColumnHeader2.Width = 100 " "ColumnHeader3 " Me.ColumnHeader3.Text = "Item3" Me.ColumnHeader3.Width = 93 " "ColumnHeader4 " Me.ColumnHeader4.Text = "Item4" Me.ColumnHeader4.Width = 100 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(376, 134) Me.Controls.Add(Me.ListView1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Friend WithEvents ListView1 As System.Windows.Forms.ListView 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 ColumnHeader4 As System.Windows.Forms.ColumnHeader
End Class
</source>