| 对窗体的更改
通常情况下,要在升序和降序排序之间切换,您要多次单击列标题。用户期望如果他们单击列标题,排序将会发生,随后再次单击将更改排序顺序。前面的代码示例需要能够确定何时多次单击列。为此,您可以将一个私有整数变量添加到 Form 类。这个变量存储上一次单击的列。ColumnClick 事件处理方法将使用这个变量来比较上一次的列与当前单击的列,并确定它们是否相同。将以下成员定义添加到 Form 类中。
''''Visual Basic
Dim sortColumn as Integer = -1
//C#
private int sortColumn = -1;
ColumnClick 事件处理方法的更改
在前面的示例中定义的 ColumnClick 事件处理方法需要进行修改,以便跟踪单击过的列和当前排序顺序。添加以下代码以替换在前面的示例中创建的 ColumnClick 事件处理方法中的代码。
''''Visual Basic
Private Sub listView1_ColumnClick(sender As Object, e As
System.Windows.Forms.ColumnClickEventArgs)
'''' Determine whether the column is the same as the last column clicked.
If e.Column <> sortColumn Then
'''' Set the sort column to the new column.
sortColumn = e.Column
'''' Set the sort order to ascending by default.
listView1.Sorting = SortOrder.Ascending
Else
'''' Determine what the last sort order was and change it.
If listView1.Sorting = SortOrder.Ascending Then
listView1.Sorting = SortOrder.Descending
Else
listView1.Sorting = SortOrder.Ascending
End If
End If
'''' Call the sort method to manually sort.
listView1.Sort()
'''' Set the ListViewItemSorter property to a new ListViewItemComparer
'''' object.
listView1.ListViewItemSorter = New ListViewItemComparer(e.Column, _
listView1.Sorting) 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 |