| //C#
this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_ColumnClick);
将下面的代码添加到用于 ColumnClick 事件的事件处理方法。
''''Visual Basic
'''' Set the ListViewItemSorter property to a new ListViewItemComparer
'''' object.
Me.listView1.ListViewItemSorter = New ListViewItemComparer(e.Column)
'''' Call the sort method to manually sort.
listView1.Sort()
//C#
// Set the ListViewItemSorter property to a new ListViewItemComparer
// object.
this.listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
// Call the sort method to manually sort.
listView1.Sort();
添加到事件处理方法的代码会利用 ListViewItemComparer 类(在下一部分中定义)的新实例来设置 ListView 控件的 ListViewItemSorter 属性,然后分配要单击的列。被单击的列作为事件参数的组成部分进行传递。在设置 ListViewItemSorter 属性后,调用 Sort 方法来执行手动排序。
创建 ListViewItemComparer 类
正如前面提到的那样,在 ListView 控件中进行自定义排序的关键在于创建实现 System.Collections.IComparer 接口的类。就是这个类为项目提供排序。对于这个示例,定义了名为 ListViewItemComparer 的类,并且将其添加为窗体的嵌套类。ListViewItemComparer 执行传递到其构造函数的指定列的基本升序排序。将以下类定义添加到 Form 类并确保它在窗体内是正确嵌套的。
''''Visual Basic
'''' Implements the manual sorting of items by column.
Class ListViewItemComparer
Implements IComparer
Private col As Integer
Public Sub New()
col = 0
End Sub
Public Sub New(column As Integer)
col = column
End Sub
Public Function Compare(x As Object, y As Object) As Integer _
&nb 上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 |