Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 06-January-2016
- ' ***********************************************************************
- #Region " Public Members Summary "
- #Region " Constructors "
- ' New()
- #End Region
- #Region " Properties "
- ' ColumnIndex As Integer
- ' Order As SortOrder
- ' SortModifier As SortModifiers
- #End Region
- #Region " Functions "
- ' Compare(Object, Object) As Integer : Implements IComparer.Compare
- #End Region
- #End Region
- #Region " Usage Examples "
- ' Public Class Form1 : Inherits Form
- '
- ' Friend WithEvents MyListView As New ListView
- ' Private sorter As New ListViewColumnSorter
- '
- ' Public Sub New()
- '
- ' MyClass.InitializeComponent()
- '
- ' With Me.MyListView
- ' ' Set the sorter, our ListViewColumnSorter.
- ' .ListViewItemSorter = sorter
- '
- ' ' The initial direction for the sorting.
- ' .Sorting = SortOrder.Ascending
- '
- ' ' Set the inivial sort-modifier.
- ' sorter.SortModifier = SortModifiers.SortByText
- '
- ' ' Add some columns.
- ' .Columns.Add("Text").Tag = SortModifiers.SortByText
- ' .Columns.Add("Numbers").Tag = SortModifiers.SortByNumber
- ' .Columns.Add("Dates").Tag = SortModifiers.SortByDate
- '
- ' ' Adjust the column sizes.
- ' For Each col As ColumnHeader In Me.MyListView.Columns
- ' col.Width = 100
- ' Next
- '
- ' ' Add some items.
- ' .Items.Add("hello").SubItems.AddRange({"2", "11/11/2000"})
- ' .Items.Add("yeehaa!").SubItems.AddRange({"1", "9/9/1999"})
- ' .Items.Add("El3ktr0").SubItems.AddRange({"100", "21/08/2014"})
- ' .Items.Add("wow").SubItems.AddRange({"10", "11-11-2000"})
- '
- ' ' Styling things.
- ' .Dock = DockStyle.Fill
- ' .View = View.Details
- ' .FullRowSelect = True
- ' End With
- '
- ' With Me ' Styling things.
- ' .Size = New Size(400, 200)
- ' .FormBorderStyle = Global.System.Windows.Forms.FormBorderStyle.FixedSingle
- ' .MaximizeBox = False
- ' .StartPosition = FormStartPosition.CenterScreen
- ' .Text = "ListViewColumnSorter TestForm"
- ' End With
- '
- ' Me.Controls.Add(Me.MyListView)
- '
- ' End Sub
- '
- ' ''' ----------------------------------------------------------------------------------------------------
- ' ''' <summary>
- ' ''' Handles the <see cref="ListView.ColumnClick"/> event of the <see cref="MyListView"/> control.
- ' ''' </summary>
- ' ''' ----------------------------------------------------------------------------------------------------
- ' ''' <param name="sender">
- ' ''' The source of the event.
- ' ''' </param>
- ' '''
- ' ''' <param name="e">
- ' ''' The <see cref="ColumnClickEventArgs"/> instance containing the event data.
- ' ''' </param>
- ' ''' ----------------------------------------------------------------------------------------------------
- ' Private Sub MyListView_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) _
- ' Handles MyListView.ColumnClick
- '
- ' Dim lv As ListView = DirectCast(sender, ListView)
- '
- ' ' Dinamycaly sets the sort-modifier to sort the column by text, number, or date.
- ' sorter.SortModifier = DirectCast(lv.Columns(e.Column).Tag, SortModifiers)
- '
- ' ' Determine whether clicked column is already the column that is being sorted.
- ' If (e.Column = sorter.ColumnIndex) Then
- '
- ' ' Reverse the current sort direction for this column.
- ' If (sorter.Order = SortOrder.Ascending) Then
- ' sorter.Order = SortOrder.Descending
- '
- ' Else
- ' sorter.Order = SortOrder.Ascending
- '
- ' End If ' Sorter.Order
- '
- ' Else
- ' ' Set the column number that is to be sorted, default to ascending.
- ' sorter.ColumnIndex = e.Column
- ' sorter.Order = SortOrder.Ascending
- '
- ' End If ' e.Column
- '
- ' ' Perform the sort.
- ' lv.Sort()
- '
- ' End Sub
- '
- ' End Class
- #End Region
- #Region " Option Statements "
- Option Strict On
- Option Explicit On
- Option Infer Off
- #End Region
- #Region " Imports "
- Imports System
- Imports System.Collections
- Imports System.ComponentModel
- Imports System.Diagnostics
- Imports System.Linq
- Imports System.Windows.Forms
- Imports Elektro.Application.UI.Enums
- #End Region
- #Region " ListView's Column Sorter "
- Namespace Types
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Performs a sorting operation in a <see cref="System.Windows.Forms.ListView"/>.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <example> This is a code example.
- ''' <code>
- ''' Public Class Form1 : Inherits Form
- '''
- ''' Friend WithEvents MyListView As New ListView
- ''' Private sorter As New ListViewColumnSorter
- '''
- ''' Public Sub New()
- '''
- ''' MyClass.InitializeComponent()
- '''
- ''' With Me.MyListView
- ''' ' Set the sorter, our ListViewColumnSorter.
- ''' .ListViewItemSorter = sorter
- '''
- ''' ' The initial direction for the sorting.
- ''' .Sorting = SortOrder.Ascending
- '''
- ''' ' Set the inivial sort-modifier.
- ''' sorter.SortModifier = SortModifiers.SortByText
- '''
- ''' ' Add some columns.
- ''' .Columns.Add("Text").Tag = SortModifiers.SortByText
- ''' .Columns.Add("Numbers").Tag = SortModifiers.SortByNumber
- ''' .Columns.Add("Dates").Tag = SortModifiers.SortByDate
- '''
- ''' ' Adjust the column sizes.
- ''' For Each col As ColumnHeader In Me.MyListView.Columns
- ''' col.Width = 100
- ''' Next
- '''
- ''' ' Add some items.
- ''' .Items.Add("hello").SubItems.AddRange({"2", "11/11/2000"})
- ''' .Items.Add("yeehaa!").SubItems.AddRange({"1", "9/9/1999"})
- ''' .Items.Add("El3ktr0").SubItems.AddRange({"100", "21/08/2014"})
- ''' .Items.Add("wow").SubItems.AddRange({"10", "11-11-2000"})
- '''
- ''' ' Styling things.
- ''' .Dock = DockStyle.Fill
- ''' .View = View.Details
- ''' .FullRowSelect = True
- ''' End With
- '''
- ''' With Me ' Styling things.
- ''' .Size = New Size(400, 200)
- ''' .FormBorderStyle = Global.System.Windows.Forms.FormBorderStyle.FixedSingle
- ''' .MaximizeBox = False
- ''' .StartPosition = FormStartPosition.CenterScreen
- ''' .Text = "ListViewColumnSorter TestForm"
- ''' End With
- '''
- ''' Me.Controls.Add(Me.MyListView)
- '''
- ''' End Sub
- '''
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <summary>
- ''' ''' Handles the <see cref="ListView.ColumnClick"/> event of the <see cref="MyListView"/> control.
- ''' ''' </summary>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' ''' <param name="sender">
- ''' ''' The source of the event.
- ''' ''' </param>
- ''' '''
- ''' ''' <param name="e">
- ''' ''' The <see cref="ColumnClickEventArgs"/> instance containing the event data.
- ''' ''' </param>
- ''' ''' ----------------------------------------------------------------------------------------------------
- ''' Private Sub MyListView_ColumnClick(ByVal sender As Object, ByVal e As ColumnClickEventArgs) _
- ''' Handles MyListView.ColumnClick
- '''
- ''' Dim lv As ListView = DirectCast(sender, ListView)
- '''
- ''' ' Dinamycaly sets the sort-modifier to sort the column by text, number, or date.
- ''' sorter.SortModifier = DirectCast(lv.Columns(e.Column).Tag, SortModifiers)
- '''
- ''' ' Determine whether clicked column is already the column that is being sorted.
- ''' If (e.Column = sorter.ColumnIndex) Then
- '''
- ''' ' Reverse the current sort direction for this column.
- ''' If (sorter.Order = SortOrder.Ascending) Then
- ''' sorter.Order = SortOrder.Descending
- '''
- ''' Else
- ''' sorter.Order = SortOrder.Ascending
- '''
- ''' End If ' Sorter.Order
- '''
- ''' Else
- ''' ' Set the column number that is to be sorted, default to ascending.
- ''' sorter.ColumnIndex = e.Column
- ''' sorter.Order = SortOrder.Ascending
- '''
- ''' End If ' e.Column
- '''
- ''' ' Perform the sort.
- ''' lv.Sort()
- '''
- ''' End Sub
- '''
- ''' End Class
- ''' </code>
- ''' </example>
- ''' ----------------------------------------------------------------------------------------------------
- Public NotInheritable Class ListViewColumnSorter : Implements IComparer
- #Region " Private Fields "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The comparer instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private comparer As IComparer
- #End Region
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the index of the column to which to apply the sorting operation (default index is <c>0</c>).
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The index of the column to which to apply the sorting operation (default index is <c>0</c>).
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property ColumnIndex As Integer
- <DebuggerStepThrough>
- Get
- Return Me.columnIndexB
- End Get
- <DebuggerStepThrough>
- Set(ByVal value As Integer)
- Me.columnIndexB = value
- End Set
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing field )
- ''' The index of the column to which to apply the sorting operation (default index is <c>0</c>).
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private columnIndexB As Integer
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the order of sorting to apply.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The order of sorting to apply.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property Order As SortOrder
- <DebuggerStepThrough>
- Get
- Return Me.orderB
- End Get
- <DebuggerStepThrough>
- Set(ByVal value As SortOrder)
- Me.orderB = value
- End Set
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing field )
- ''' The order of sorting to apply.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private orderB As SortOrder
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets the sort modifier.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The sort modifier.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property SortModifier As SortModifiers
- <DebuggerStepThrough>
- Get
- Return Me.sortModifierB
- End Get
- <DebuggerStepThrough>
- Set(ByVal value As SortModifiers)
- Me.sortModifierB = value
- End Set
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing field )
- ''' The sort modifier.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private sortModifierB As SortModifiers
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="ListViewColumnSorter"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerNonUserCode>
- Public Sub New()
- Me.comparer = New TextComparer
- Me.columnIndexB = 0
- Me.orderB = SortOrder.None
- Me.sortModifierB = SortModifiers.SortByText
- End Sub
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="a">
- ''' The first object to compare.
- ''' </param>
- '''
- ''' <param name="b">
- ''' The second object to compare.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' A signed integer that indicates the relative values of <paramref name="a"/> and <paramref name="b"/>.
- ''' <para></para>
- ''' 0: <paramref name="a"/> equals <paramref name="b"/>.
- ''' <para></para>
- ''' Less than 0: <paramref name="a"/> is less than <paramref name="b"/>.
- ''' <para></para>
- ''' Greater than 0: <paramref name="a"/> is greater than <paramref name="b"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements IComparer.Compare
- Dim compareResult As ComparerResult = ComparerResult.Equals
- Dim lvItemA As ListViewItem
- Dim lvItemB As ListViewItem
- ' Cast the objects to be compared
- lvItemA = DirectCast(a, ListViewItem)
- lvItemB = DirectCast(b, ListViewItem)
- Dim strA As String = If(Not lvItemA.SubItems.Count <= Me.columnIndexB,
- lvItemA.SubItems(Me.columnIndexB).Text,
- Nothing)
- Dim strB As String = If(Not lvItemB.SubItems.Count <= Me.columnIndexB,
- lvItemB.SubItems(Me.columnIndexB).Text,
- Nothing)
- Dim listViewMain As ListView = lvItemA.ListView
- ' Calculate correct return value based on object comparison
- If listViewMain.Sorting <> SortOrder.Ascending AndAlso listViewMain.Sorting <> SortOrder.Descending Then
- ' Return '0' to indicate they are equal
- Return ComparerResult.Equals
- End If
- If Me.sortModifierB.Equals(SortModifiers.SortByText) Then
- ' Compare the two items
- If lvItemA.SubItems.Count <= Me.columnIndexB AndAlso lvItemB.SubItems.Count <= Me.columnIndexB Then
- compareResult = DirectCast(Me.comparer.Compare(Nothing, Nothing), ComparerResult)
- ElseIf lvItemA.SubItems.Count <= Me.columnIndexB AndAlso lvItemB.SubItems.Count > Me.columnIndexB Then
- compareResult = DirectCast(Me.comparer.Compare(Nothing, strB), ComparerResult)
- ElseIf lvItemA.SubItems.Count > Me.columnIndexB AndAlso lvItemB.SubItems.Count <= Me.columnIndexB Then
- compareResult = DirectCast(Me.comparer.Compare(strA, Nothing), ComparerResult)
- Else
- compareResult = DirectCast(Me.comparer.Compare(strA, strB), ComparerResult)
- End If
- Else ' Me.sortModifierB IsNot SortModifiers.SortByText.
- Select Case Me.sortModifierB
- Case SortModifiers.SortByNumber
- If Me.comparer.GetType <> GetType(NumericComparer) Then
- Me.comparer = New NumericComparer
- End If
- Case SortModifiers.SortByDate
- If Me.comparer.GetType <> GetType(DateComparer) Then
- Me.comparer = New DateComparer
- End If
- Case Else
- If Me.comparer.GetType <> GetType(TextComparer) Then
- Me.comparer = New TextComparer
- End If
- End Select
- compareResult = DirectCast(Me.comparer.Compare(strA, strB), ComparerResult)
- End If ' Me.sortModifierB.Equals(...)
- ' Calculate correct return value based on object comparison.
- If Me.orderB = SortOrder.Ascending Then
- ' Ascending sort is selected, return normal result of compare operation.
- Return compareResult
- ElseIf Me.orderB = SortOrder.Descending Then
- ' Descending sort is selected, return negative result of compare operation.
- Return -CInt(compareResult)
- Else
- ' Return '0' to indicate they are equal.
- Return 0
- End If ' Me.orderB = ...
- End Function
- #End Region
- #Region " Hidden Base Members "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether the specified <see cref="Object"/> is equal to this instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="obj">
- ''' Another object to compare to.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if the specified <see cref="Object"/> is equal to this instance; otherwise, <see langword="False"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- <DebuggerNonUserCode>
- Public Shadows Function Equals(ByVal obj As Object) As Boolean
- Return MyBase.Equals(obj)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether the specified <see cref="System.Object"/> instances are the same instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="objA">
- ''' The first object to compare.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="objB">
- ''' The second object to compare.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if <paramref name="objA"/> is the same instance as <paramref name="objB"/>
- ''' or if both are <see langword="Nothing"/>; otherwise, <see langword="False"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- <DebuggerNonUserCode>
- Public Shadows Function ReferenceEquals(ByVal objA As Object, ByVal objB As Object) As Boolean
- Return Object.ReferenceEquals(objA, objB)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the <see cref="System.Type"/> of the current instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The exact runtime type of the current instance.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- <DebuggerNonUserCode>
- Public Shadows Function [GetType]() As Type
- Return MyBase.GetType
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Serves as a hash function for a particular type.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- <DebuggerNonUserCode>
- Public Shadows Function GetHashCode() As Integer
- Return MyBase.GetHashCode
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Returns a String that represents the current object.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' A string that represents the current object.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <EditorBrowsable(EditorBrowsableState.Never)>
- <DebuggerNonUserCode>
- Public Shadows Function ToString() As String
- Return MyBase.ToString
- End Function
- #End Region
- End Class
- End Namespace
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement