Gets or sets the sorting comparer for the control.
Syntax
Visual Basic (Usage) | Copy Code |
---|
Dim instance As RasterImageList
Dim value As IComparer
instance.Sorter = value
value = instance.Sorter
|
Return Value
An IComparer that represents the sorting comparer for the control.
Example
This example will create and populate a <see cref="RasterImageList"/> control with 20 items. It will use a
custom <see cref="IComparer"/> that implements the Win32 <i>StrCmpLogicalW</i> API to sort the contents of the control.
This example assumes that you have added the code to a Form and call the method created in the example from the
constructor or another method on the form.<br/>
<b>Note:</b> This example will only run on Windows XP and Windows 2003 Server.
Visual Basic | Copy Code |
---|
Public Sub RasterImageList_Sorter(ByVal imageList As RasterImageList)
imageList.Bounds = New Rectangle(New Point(10, 10), New Size(400, 400))
imageList.Sorting = SortOrder.Ascending
imageList.ViewStyle = RasterImageListViewStyle.Normal
imageList.BeginUpdate()
imageList.Items.Clear()
For i As Integer = 0 To 19
Dim item As RasterImageListItem = New RasterImageListItem(Nothing, i, i.ToString())
imageList.Items.Add(item)
Next i
imageList.EndUpdate()
imageList.Sort()
MessageBox.Show("Default sort (0, 1, 10, 11, etc...)")
imageList.Sorter = New MyRasterImageListItemComparer()
MessageBox.Show("Custom sort (0, 1, 2, 3, etc... ")
End Sub
<DllImport("SHLWAPI.DLL", EntryPoint:="StrCmpLogicalW", SetLastError:=True, CharSet:=CharSet.Unicode, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function StrCmpLogicalW(ByVal psz1 As String, ByVal psz2 As String) As Integer
End Function
Private Class MyRasterImageListItemComparer : Implements IComparer
Public Sub New()
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim itemX As RasterImageListItem = CType(IIf(TypeOf x Is RasterImageListItem, x, Nothing), RasterImageListItem)
Dim itemY As RasterImageListItem = CType(IIf(TypeOf y Is RasterImageListItem, y, Nothing), RasterImageListItem)
Return StrCmpLogicalW(itemX.Text, itemY.Text)
End Function
End Class |
C# | Copy Code |
---|
public void RasterImageList_Sorter(RasterImageList imageList) { imageList.Bounds = new Rectangle(new Point(10, 10), new Size(400, 400)); // Sort the items in the list in ascending order. imageList.Sorting = SortOrder.Ascending; // Use normal view style imageList.ViewStyle = RasterImageListViewStyle.Normal; // Suspend painting the RasterImageList imageList.BeginUpdate(); // Clear the image list imageList.Items.Clear(); // Add 20 items for(int i = 0; i < 20; i++) { // Use the item index as its text RasterImageListItem item = new RasterImageListItem(null, i, i.ToString()); imageList.Items.Add(item); } // Resume painting imageList.EndUpdate(); // Sort using the default sorter imageList.Sort(); MessageBox.Show("Default sort (0, 1, 10, 11, etc...)"); // Setup custom sorting imageList.Sorter = new MyRasterImageListItemComparer(); MessageBox.Show("Custom sort (0, 1, 2, 3, etc... "); } [DllImport("SHLWAPI.DLL", EntryPoint = "StrCmpLogicalW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern int StrCmpLogicalW(string psz1, string psz2); class MyRasterImageListItemComparer : IComparer { public MyRasterImageListItemComparer() { } public int Compare(object x, object y) { RasterImageListItem itemX = x as RasterImageListItem; RasterImageListItem itemY = y as RasterImageListItem; return StrCmpLogicalW(itemX.Text, itemY.Text); } } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 2.0, Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also