Construct ListView at run time: build structure and insert records
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.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.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.components)
'Me.imlLarge = New System.Windows.Forms.ImageList(Me.components)
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
Related examples in the same category