VB.Net by API/System.Windows.Forms/ListViewItem
Версия от 16:40, 26 мая 2010; (обсуждение)
ListViewItem.ImageIndex
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
ListViewItem.SubItems.Add
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ListViewIllustration
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
Dim intWidth As Integer
Dim objItem As ListViewItem
"Set the default view.
ListView1.View = View.Details
optDetails.Checked = True
"Add the column headers.
intWidth = ListView1.Width - 5
ListView1.Columns.Add("Name", CInt(intWidth / 4))
ListView1.Columns.Add("Address", CInt(intWidth / 4))
ListView1.Columns.Add("Phone", CInt(intWidth / 4))
ListView1.Columns.Add("FAX", CInt(intWidth / 4))
"Add some list view items.
objItem = ListView1.Items.Add("AAAAA")
With objItem
.SubItems.Add("123 Main St.")
.SubItems.Add("555-555-5555")
.SubItems.Add("555-555-5555")
.ImageIndex = 0
End With
objItem = ListView1.Items.Add("BBBBB")
With objItem
.SubItems.Add("456 Main St.")
.SubItems.Add("555-555-5555")
.SubItems.Add("555-555-5555")
.ImageIndex = 0
End With
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim strItemText As String
Dim objItem As ListViewItem
"Add some list view items.
strItemText = "name:"
objItem = ListView1.Items.Add(strItemText)
With objItem
.SubItems.Add("123 Some St.")
.SubItems.Add("555-555-5555")
.SubItems.Add("555-555-5555")
.ImageIndex = 0
End With
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ListView1.Items.Clear()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim objListItem As ListViewItem
For Each objListItem In ListView1.SelectedItems
objListItem.Remove()
Next objListItem
End Sub
Private Sub btnDisplayItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayItem.Click
Dim strMessage As String
Dim objListItem As ListViewItem
If ListView1.SelectedItems.Count > 0 Then
objListItem = ListView1.SelectedItems(0)
With objListItem
strMessage = "NAME: " & .Text & vbCrLf & _
"ADDRESS: " & .SubItems(1).Text & vbCrLf & _
"PHONE: " & .SubItems(2).Text & vbCrLf & _
"FAX: " & .SubItems(3).Text
End With
MsgBox(strMessage)
End If
End Sub
Private Sub optLargeIcon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optLargeIcon.CheckedChanged
ListView1.View = View.LargeIcon
End Sub
Private Sub optSmallIcon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optSmallIcon.CheckedChanged
ListView1.View = View.SmallIcon
End Sub
Private Sub optList_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optList.CheckedChanged
ListView1.View = View.List
End Sub
Private Sub optTile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optTile.CheckedChanged
ListView1.View = View.Tile
End Sub
Private Sub optDetails_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDetails.CheckedChanged
ListView1.View = View.Details
End Sub
End Class
Partial Public Class Form1
Inherits System.Windows.Forms.Form
<System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: The following procedure is required by the Windows Form Designer
"It can be modified using the Windows Form Designer.
"Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.ruponents = New System.ruponentModel.Container
Me.ListView1 = New System.Windows.Forms.ListView
Me.btnPopulate = New System.Windows.Forms.Button
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.optDetails = New System.Windows.Forms.RadioButton
Me.optTile = New System.Windows.Forms.RadioButton
Me.optList = New System.Windows.Forms.RadioButton
Me.optSmallIcon = New System.Windows.Forms.RadioButton
Me.optLargeIcon = New System.Windows.Forms.RadioButton
Me.btnAdd = New System.Windows.Forms.Button
Me.btnRemove = New System.Windows.Forms.Button
Me.btnClear = New System.Windows.Forms.Button
Me.btnDisplayItem = New System.Windows.Forms.Button
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
"
"ListView1
"
Me.ListView1.FullRowSelect = True
Me.ListView1.Location = New System.Drawing.Point(17, 18)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(342, 364)
Me.ListView1.TabIndex = 0
Me.ListView1.UseCompatibleStateImageBehavior = False
"
"btnPopulate
"
Me.btnPopulate.Location = New System.Drawing.Point(366, 239)
Me.btnPopulate.Name = "btnPopulate"
Me.btnPopulate.Size = New System.Drawing.Size(161, 23)
Me.btnPopulate.TabIndex = 2
Me.btnPopulate.Text = "Populate"
"
"GroupBox1
"
Me.GroupBox1.Controls.Add(Me.optDetails)
Me.GroupBox1.Controls.Add(Me.optTile)
Me.GroupBox1.Controls.Add(Me.optList)
Me.GroupBox1.Controls.Add(Me.optSmallIcon)
Me.GroupBox1.Controls.Add(Me.optLargeIcon)
Me.GroupBox1.Location = New System.Drawing.Point(370, 13)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(156, 166)
Me.GroupBox1.TabIndex = 1
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Views"
"
"optDetails
"
Me.optDetails.AutoSize = True
Me.optDetails.Location = New System.Drawing.Point(30, 126)
Me.optDetails.Name = "optDetails"
Me.optDetails.Size = New System.Drawing.Size(57, 17)
Me.optDetails.TabIndex = 4
Me.optDetails.Text = "Details"
"
"optTile
"
Me.optTile.AutoSize = True
Me.optTile.Location = New System.Drawing.Point(30, 102)
Me.optTile.Name = "optTile"
Me.optTile.Size = New System.Drawing.Size(42, 17)
Me.optTile.TabIndex = 3
Me.optTile.Text = "Tile"
"
"optList
"
Me.optList.AutoSize = True
Me.optList.Location = New System.Drawing.Point(30, 78)
Me.optList.Name = "optList"
Me.optList.Size = New System.Drawing.Size(41, 17)
Me.optList.TabIndex = 2
Me.optList.Text = "List"
"
"optSmallIcon
"
Me.optSmallIcon.AutoSize = True
Me.optSmallIcon.Location = New System.Drawing.Point(30, 54)
Me.optSmallIcon.Name = "optSmallIcon"
Me.optSmallIcon.Size = New System.Drawing.Size(79, 17)
Me.optSmallIcon.TabIndex = 1
Me.optSmallIcon.Text = "Small Icons"
"
"optLargeIcon
"
Me.optLargeIcon.AutoSize = True
Me.optLargeIcon.Location = New System.Drawing.Point(30, 30)
Me.optLargeIcon.Name = "optLargeIcon"
Me.optLargeIcon.Size = New System.Drawing.Size(81, 17)
Me.optLargeIcon.TabIndex = 0
Me.optLargeIcon.Text = "Large Icons"
"
"btnAdd
"
Me.btnAdd.Location = New System.Drawing.Point(365, 269)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(161, 23)
Me.btnAdd.TabIndex = 3
Me.btnAdd.Text = "Add"
"
"btnRemove
"
Me.btnRemove.Location = New System.Drawing.Point(366, 299)
Me.btnRemove.Name = "btnRemove"
Me.btnRemove.Size = New System.Drawing.Size(161, 23)
Me.btnRemove.TabIndex = 4
Me.btnRemove.Text = "Remove"
"
"btnClear
"
Me.btnClear.Location = New System.Drawing.Point(365, 329)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(161, 23)
Me.btnClear.TabIndex = 5
Me.btnClear.Text = "Clear"
"
"btnDisplayItem
"
Me.btnDisplayItem.Location = New System.Drawing.Point(365, 359)
Me.btnDisplayItem.Name = "btnDisplayItem"
Me.btnDisplayItem.Size = New System.Drawing.Size(161, 23)
Me.btnDisplayItem.TabIndex = 6
Me.btnDisplayItem.Text = "Display Item"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(539, 394)
Me.Controls.Add(Me.btnDisplayItem)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnRemove)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.btnPopulate)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "ListView"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents btnPopulate As System.Windows.Forms.Button
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents optDetails As System.Windows.Forms.RadioButton
Friend WithEvents optTile As System.Windows.Forms.RadioButton
Friend WithEvents optList As System.Windows.Forms.RadioButton
Friend WithEvents optSmallIcon As System.Windows.Forms.RadioButton
Friend WithEvents optLargeIcon As System.Windows.Forms.RadioButton
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents btnRemove As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents btnDisplayItem As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
End Class