VB.Net Tutorial/GUI/ListView Sort — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:57, 26 мая 2010
ListView custom sort
"Visual Basic 2005 Programmer"s Reference
"by Rod Stephens (Author)
"# Publisher: Wrox (October 21, 2005)
"# Language: English
"# ISBN-10: 0764571982
"# ISBN-13: 978-0764571985
Imports System.Windows.Forms
Imports System.Collections
public class ListViewCustomSort
public Shared Sub Main
Application.Run(New 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(lvwBooks, _
"Title", HorizontalAlignment.Left, 120, _
"URL", HorizontalAlignment.Left, 120, _
"ISBN", HorizontalAlignment.Left, 90, _
"Picture", HorizontalAlignment.Left, 120, _
"Pages", HorizontalAlignment.Right, 50, _
"Year", HorizontalAlignment.Right, 40)
ListViewMakeRow(lvwBooks, 0, _
"Visual Basic and XML", _
"http://www.vb-helper.ru/xml.htm", _
"0-471-12060-X", _
"http://www.vb-helper.ru/xml.jpg", _
"503", _
"2002")
ListViewMakeRow(lvwBooks, 0, _
"Visual Basic Graphics Programming, 2e", _
"http://www.vb-helper.ru/vbgp.htm", _
"0-471-35599-2", _
"http://www.vb-helper.ru/vbgp.jpg", _
"712", _
"2000")
ListViewMakeRow(lvwBooks, 0, _
"Ready-to-Run Visual Basic Algorithms", _
"http://www.vb-helper.ru/vba.htm", _
"0-471-24268-3", _
"http://www.vb-helper.ru/vba.jpg", _
"395", _
"1998")
ListViewSizeColumns(lvwBooks, True)
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
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
lvwBooks.View = View.Details
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
End Sub
Private Sub mnuViewLargeIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewLargeIcons.Click
lvwBooks.View = View.LargeIcon
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
End Sub
Private Sub mnuViewList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewList.Click
lvwBooks.View = View.List
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
End Sub
Private Sub mnuViewSmallIcons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewSmallIcons.Click
lvwBooks.View = View.SmallIcon
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
End Sub
Private Sub mnuViewTile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewTile.Click
lvwBooks.View = View.Tile
Text = "RunTimeListView (" & lvwBooks.View.ToString & ")"
End Sub
" Sort using the clicked column.
Private Sub lvwBooks_ColumnClick(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwBooks.ColumnClick
" Get the new sorting column.
Dim new_sorting_column As ColumnHeader = _
lvwBooks.Columns(e.Column)
" Figure out the new sorting order.
Dim sort_order As System.Windows.Forms.SortOrder
If m_SortingColumn Is Nothing Then
" New column. Sort ascending.
sort_order = SortOrder.Ascending
Else
" See if this is the same column.
If new_sorting_column.Equals(m_SortingColumn) Then
" Same column. Switch the sort order.
If m_SortingColumn.Text.StartsWith("> ") Then
sort_order = SortOrder.Descending
Else
sort_order = SortOrder.Ascending
End If
Else
" New column. Sort ascending.
sort_order = SortOrder.Ascending
End If
" Remove the old sort indicator.
m_SortingColumn.Text = m_SortingColumn.Text.Substring(2)
End If
" Display the new sort order.
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
" Create a comparer.
lvwBooks.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
" Sort.
lvwBooks.Sort()
End Sub
End Class
" Implements a comparer for ListView columns.
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
" Compare the items in the appropriate column
" for objects x and y.
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
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.lvwBooks = 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()
"
"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"
"
"lvwBooks
"
Me.lvwBooks.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3})
Me.lvwBooks.Dock = System.Windows.Forms.DockStyle.Fill
Me.lvwBooks.Location = New System.Drawing.Point(0, 24)
Me.lvwBooks.Name = "lvwBooks"
Me.lvwBooks.Size = New System.Drawing.Size(772, 249)
Me.lvwBooks.SmallImageList = Me.imlSmall
Me.lvwBooks.TabIndex = 2
Me.lvwBooks.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.lvwBooks)
Me.Controls.Add(Me.MenuStrip1)
Me.Name = "Form1"
Me.Text = "ListViewCustomSort"
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 imlSmall As System.Windows.Forms.ImageList
Friend WithEvents lvwBooks 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
Sort a listview
Imports System.Windows.Forms
public class ListViewSorting
public Shared Sub Main
Application.Run(New 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
Dim mycount As Integer
mycount = listview1.Items.Count
ToolStripStatusLabel1.Text = Str(mycount)
End Sub
Private Sub ListView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.Click
Dim i As Integer
Dim mycount As Integer
mycount = ListView1.Items.Count
For i = 0 To mycount - 1
If ListView1.Items(i).Selected = True Then
ToolStripStatusLabel1.Text = ListView1.Items(i).Text + " selected"
Exit For
End If
Next
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
ListView1.Sorting = SortOrder.Ascending
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
ListView1.Sorting = SortOrder.Descending
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
ListView1.Sorting = SortOrder.None
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
"Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected 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("Item1", 0)
Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item2", 1)
Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item3", 2)
Dim ListViewItem4 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item4", 3)
Dim ListViewItem5 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem("Item5", 4)
Me.ListView1 = New System.Windows.Forms.ListView
Me.RadioButton1 = New System.Windows.Forms.RadioButton
Me.RadioButton2 = New System.Windows.Forms.RadioButton
Me.RadioButton3 = New System.Windows.Forms.RadioButton
Me.StatusStrip1 = New System.Windows.Forms.StatusStrip
Me.ToolStripStatusLabel1 = New System.Windows.Forms.ToolStripStatusLabel
Me.StatusStrip1.SuspendLayout()
Me.SuspendLayout()
"
"ListView1
"
Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3, ListViewItem4, ListViewItem5})
Me.ListView1.LargeImageList = Me.ImageList1
Me.ListView1.Location = New System.Drawing.Point(16, 15)
Me.ListView1.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(317, 155)
Me.ListView1.TabIndex = 0
Me.ListView1.UseCompatibleStateImageBehavior = False
"
"RadioButton1
"
Me.RadioButton1.AutoSize = True
Me.RadioButton1.Location = New System.Drawing.Point(391, 39)
Me.RadioButton1.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.RadioButton1.Name = "RadioButton1"
Me.RadioButton1.Size = New System.Drawing.Size(58, 19)
Me.RadioButton1.TabIndex = 1
Me.RadioButton1.TabStop = True
Me.RadioButton1.Text = "ascending"
Me.RadioButton1.UseVisualStyleBackColor = True
"
"RadioButton2
"
Me.RadioButton2.AutoSize = True
Me.RadioButton2.Location = New System.Drawing.Point(391, 79)
Me.RadioButton2.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.RadioButton2.Name = "RadioButton2"
Me.RadioButton2.Size = New System.Drawing.Size(58, 19)
Me.RadioButton2.TabIndex = 2
Me.RadioButton2.TabStop = True
Me.RadioButton2.Text = "descending"
Me.RadioButton2.UseVisualStyleBackColor = True
"
"RadioButton3
"
Me.RadioButton3.AutoSize = True
Me.RadioButton3.Location = New System.Drawing.Point(391, 124)
Me.RadioButton3.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.RadioButton3.Name = "RadioButton3"
Me.RadioButton3.Size = New System.Drawing.Size(73, 19)
Me.RadioButton3.TabIndex = 3
Me.RadioButton3.TabStop = True
Me.RadioButton3.Text = "no order"
Me.RadioButton3.UseVisualStyleBackColor = True
"
"StatusStrip1
"
Me.StatusStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripStatusLabel1})
Me.StatusStrip1.Location = New System.Drawing.Point(0, 212)
Me.StatusStrip1.Name = "StatusStrip1"
Me.StatusStrip1.Padding = New System.Windows.Forms.Padding(1, 0, 19, 0)
Me.StatusStrip1.Size = New System.Drawing.Size(536, 22)
Me.StatusStrip1.TabIndex = 4
Me.StatusStrip1.Text = "StatusStrip1"
"
"ToolStripStatusLabel1
"
Me.ToolStripStatusLabel1.Name = "ToolStripStatusLabel1"
Me.ToolStripStatusLabel1.Size = New System.Drawing.Size(0, 17)
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 15.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(536, 234)
Me.Controls.Add(Me.StatusStrip1)
Me.Controls.Add(Me.RadioButton3)
Me.Controls.Add(Me.RadioButton2)
Me.Controls.Add(Me.RadioButton1)
Me.Controls.Add(Me.ListView1)
Me.Margin = New System.Windows.Forms.Padding(4, 4, 4, 4)
Me.StatusStrip1.ResumeLayout(False)
Me.StatusStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
Friend WithEvents ToolStripStatusLabel1 As System.Windows.Forms.ToolStripStatusLabel
End Class