VB.Net by API/System.Windows.Forms/ListView — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 19:40, 26 мая 2010
Содержание
ListView.Click
<source lang="vbnet"> Imports System.IO Imports System.Windows.Forms public class LoadFileDirectoryInformationTreeView
public Shared Sub Main Application.Run(New FrmListView) End Sub
End class Public Class FrmListView
Inherits Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub " display labels for current location in directory tree Friend WithEvents lblCurrent As Label Friend WithEvents lblDisplay As Label " displays contents of current directory Friend WithEvents lvwBrowser As ListView " specifies images for file icons and folder icons Friend WithEvents ilsFileFolder As ImageList
"Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container() Me.ilsFileFolder = New System.Windows.Forms.ImageList(Me.ruponents) Me.lvwBrowser = New System.Windows.Forms.ListView() Me.lblCurrent = New System.Windows.Forms.Label() Me.lblDisplay = New System.Windows.Forms.Label() Me.SuspendLayout() " "ilsFileFolder " Me.ilsFileFolder.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit Me.ilsFileFolder.ImageSize = New System.Drawing.Size(16, 16) Me.ilsFileFolder.TransparentColor = System.Drawing.Color.Transparent " "lvwBrowser " Me.lvwBrowser.Location = New System.Drawing.Point(16, 88) Me.lvwBrowser.Name = "lvwBrowser" Me.lvwBrowser.RightToLeft = System.Windows.Forms.RightToLeft.No Me.lvwBrowser.Size = New System.Drawing.Size(448, 232) Me.lvwBrowser.SmallImageList = Me.ilsFileFolder Me.lvwBrowser.TabIndex = 2 Me.lvwBrowser.View = System.Windows.Forms.View.List " "lblCurrent " Me.lblCurrent.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblCurrent.ForeColor = System.Drawing.SystemColors.WindowText Me.lblCurrent.Location = New System.Drawing.Point(16, 16) Me.lblCurrent.Name = "lblCurrent" Me.lblCurrent.Size = New System.Drawing.Size(112, 23) Me.lblCurrent.TabIndex = 0 Me.lblCurrent.Text = "Now in Directory:" " "lblDisplay " Me.lblDisplay.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.lblDisplay.ForeColor = System.Drawing.SystemColors.WindowText Me.lblDisplay.Location = New System.Drawing.Point(128, 16) Me.lblDisplay.Name = "lblDisplay" Me.lblDisplay.Size = New System.Drawing.Size(344, 56) Me.lblDisplay.TabIndex = 1 " "FrmListView " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(488, 341) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lvwBrowser, Me.lblDisplay, Me.lblCurrent}) Me.Name = "FrmListView" Me.Text = "ListViewTest" Me.ResumeLayout(False) End Sub
- End Region
Dim currentDirectory As String = Directory.GetCurrentDirectory() Private Sub lvwBrowser_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles lvwBrowser.Click If lvwBrowser.SelectedItems.Count <> 0 Then If lvwBrowser.Items(0).Selected Then Dim directoryObject As DirectoryInfo = _ New DirectoryInfo(currentDirectory) If Not (directoryObject.Parent Is Nothing) Then LoadFilesInDirectory(directoryObject.Parent.FullName) End If Else Dim chosen As String = lvwBrowser.SelectedItems(0).Text If Directory.Exists(currentDirectory & "\" & chosen) Then If currentDirectory = "C:\" Then LoadFilesInDirectory(currentDirectory & chosen) Else LoadFilesInDirectory(currentDirectory & "\" & chosen) End If End If End If lblDisplay.Text = currentDirectory End If End Sub Public Sub LoadFilesInDirectory(ByVal currentDirectoryValue As String) lvwBrowser.Items.Clear() lvwBrowser.Items.Add("Go Up One Level") currentDirectory = currentDirectoryValue Dim newCurrentDirectory As DirectoryInfo = New DirectoryInfo(currentDirectory) Dim directoryArray As DirectoryInfo() = newCurrentDirectory.GetDirectories() Dim fileArray As FileInfo() = newCurrentDirectory.GetFiles() Dim dir As DirectoryInfo For Each dir In directoryArray Dim newDirectoryItem As ListViewItem = lvwBrowser.Items.Add(dir.Name) Next Dim file As FileInfo For Each file In fileArray Dim newFileItem As ListViewItem =lvwBrowser.Items.Add(file.Name) Next End Sub Private Sub FrmListView_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load LoadFilesInDirectory(currentDirectory) lblDisplay.Text = currentDirectory End Sub
End Class
</source>
ListView.Columns.Add
<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>
ListView.ItemActivate
<source lang="vbnet"> Imports System.Windows.Forms public class TreeViewListView
public Shared Sub Main Application.Run(New Form1) End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents TreeView1 As System.Windows.Forms.TreeView Friend WithEvents Splitter1 As System.Windows.Forms.Splitter Friend WithEvents ListView1 As System.Windows.Forms.ListView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ruponents = New System.ruponentModel.Container() Me.TreeView1 = New System.Windows.Forms.TreeView() Me.Splitter1 = New System.Windows.Forms.Splitter() Me.ListView1 = New System.Windows.Forms.ListView() Me.SuspendLayout() " "TreeView1 " Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left Me.TreeView1.Name = "TreeView1" Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Letter", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("a", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("z"), New System.Windows.Forms.TreeNode("b")}), New System.Windows.Forms.TreeNode("c", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("d"), New System.Windows.Forms.TreeNode("e")})})}) Me.TreeView1.Size = New System.Drawing.Size(121, 273) Me.TreeView1.TabIndex = 0 " "Splitter1 " Me.Splitter1.Location = New System.Drawing.Point(121, 0) Me.Splitter1.Name = "Splitter1" Me.Splitter1.Size = New System.Drawing.Size(3, 273) Me.Splitter1.TabIndex = 1 Me.Splitter1.TabStop = False " "ListView1 " Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill Me.ListView1.Location = New System.Drawing.Point(124, 0) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(168, 273) Me.ListView1.TabIndex = 2 Me.ListView1.View = System.Windows.Forms.View.List " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1, Me.Splitter1, Me.TreeView1}) Me.Name = "Form1" Me.Text = "Vehicle Hierarchy" Me.ResumeLayout(False)
End Sub
- End Region
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect select case e.Node.Text case "a" listView1.Clear() listView1.Items.Add("a",3) listView1.Items.Add("b", 4) listView1.Items.Add("c", 6) case "b" listView1.Clear() listView1.Items.Add("a", 1) listView1.Items.Add("b", 2) listView1.Items.Add("c", 5) end select End Sub Private Sub ListView1_ItemActivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.ItemActivate dim strItem as String = listView1.FocusedItem.Text Console.WriteLine(strItem) End Sub
End Class
</source>
ListView.Items.Add
<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>
ListView.Items.Count
<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>
ListView.SelectedIndexChanged
<source lang="vbnet"> Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ListViewViewChange
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents ListView1 As System.Windows.Forms.ListView Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 0", 0) Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 1", 0) Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 2", 0) Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item 3", 0) Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1)) Me.ListView1 = New System.Windows.Forms.ListView Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader Me.TextBox1 = New System.Windows.Forms.TextBox Me.ruboBox1 = New System.Windows.Forms.ruboBox Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "ListView1 " Me.ListView1.AllowColumnReorder = True Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3}) Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3, ListViewItem4}) Me.ListView1.Location = New System.Drawing.Point(32, 104) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(216, 112) Me.ListView1.TabIndex = 1 " "ColumnHeader1 " Me.ColumnHeader1.Text = "Column Header 1" " "ColumnHeader2 " Me.ColumnHeader2.Text = "Column Header 2" " "ColumnHeader3 " Me.ColumnHeader3.Text = "Column Header 3" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(64, 232) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(160, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" " "ComboBox1 " Me.ruboBox1.Items.AddRange(New Object() {"Large Icon View", "Report View", "Small Icon View", "List View"}) Me.ruboBox1.Location = New System.Drawing.Point(16, 64) Me.ruboBox1.Name = "ComboBox1" Me.ruboBox1.Size = New System.Drawing.Size(121, 21) Me.ruboBox1.TabIndex = 3 " "Button1 " Me.Button1.Location = New System.Drawing.Point(144, 64) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(112, 23) Me.Button1.TabIndex = 4 Me.Button1.Text = "Show Check Boxes" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.ruboBox1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.ListView1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ListView1.View = ComboBox1.SelectedIndex End Sub Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged If ListView1.SelectedIndices.Count > 0 Then TextBox1.Text = "Item " & ListView1.SelectedIndices(0) & " was clicked." End If End Sub Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick TextBox1.Text = "Column " & (e.Column + 1) & " was clicked." End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ListView1.CheckBoxes = True End Sub Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck If e.NewValue = CheckState.Checked Then TextBox1.Text = "Item " & e.Index() & " is checked." Else TextBox1.Text = "Item " & e.Index() & " is not checked." End If End Sub
End Class
</source>
ListView.SelectedItems
<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>
ListView.View
<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>