Build ListView at run time: update title column text
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.Compare 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.Compare(string_x, string_y) Else Return String.Compare(string_y, string_x) End If End Function End Class <Global.Microsoft.VisualBasic.CompilerServices.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.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container 'Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1)) 'Me.imlLarge = New System.Windows.Forms.ImageList(Me.components) 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.components) 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