⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customcomparertestform.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:

Public Class TestForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents bttnComparerTest As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    '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.ListBox1 = New System.Windows.Forms.ListBox()
        Me.bttnComparerTest = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.ItemHeight = 18
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(240, 256)
        Me.ListBox1.TabIndex = 1
        '
        'bttnComparerTest
        '
        Me.bttnComparerTest.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.bttnComparerTest.Location = New System.Drawing.Point(24, 272)
        Me.bttnComparerTest.Name = "bttnComparerTest"
        Me.bttnComparerTest.Size = New System.Drawing.Size(208, 40)
        Me.bttnComparerTest.TabIndex = 0
        Me.bttnComparerTest.Text = "Test Custom Comparer"
        '
        'TestForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 19)
        Me.ClientSize = New System.Drawing.Size(256, 317)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.bttnComparerTest})
        Me.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "TestForm"
        Me.Text = "Custom Comparer"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Structure Person
        Dim Name As String
        Dim BDate As Date
        Dim EMail As String
    End Structure

    Private Sub TestComparers(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnComparerTest.Click
        Dim AList As New ArrayList()
        Dim p As Person
        ' POPULATE COLLECTION
        p.Name = "C Person"
        p.EMail = "PersonC@sybex.com"
        p.BDate = #1/1/1961#
        ' ADD FIRST PERSON
        If Not AList.Contains(p) Then AList.Add(p)
        p.Name = "A Person"
        p.EMail = "PersonA@sybex.com"
        p.BDate = #3/3/1961#
        ' ADD SECOND PERSON
        If Not AList.Contains(p) Then AList.Add(p)
        p.Name = "B Person"
        p.EMail = "PersonB@sybex.com"
        p.BDate = #2/2/1961#
        ' ADD THIRD PERSON
        If Not AList.Contains(p) Then AList.Add(p)
        ' PRINT COLLECTION AS IS
        Dim PEnum As IEnumerator
        PEnum = AList.GetEnumerator
        ListBox1.Items.Add("Original Collection")
        While PEnum.MoveNext
            ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
                        vbTab & CType(PEnum.Current, Person).BDate)
        End While
        ' SORT BY NAME, THEN PRINT COLLECTION
        ListBox1.Items.Add(" ")
        ListBox1.Items.Add("Collection Sorted by Name")
        ' USE THE PersonNameComparer TO SORT ARRAYLIST BY NAME FIELD
        AList.Sort(New PersonNameComparer())
        PEnum = AList.GetEnumerator
        While PEnum.MoveNext
            ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
                        vbTab & CType(PEnum.Current, Person).BDate)
        End While
        ' SORT BY AGE, THEN PRINT COLLECTION
        ListBox1.Items.Add(" ")
        ListBox1.Items.Add("Collection Sorted by Age")
        ' USE THE PersonAgeComparer TO SORT ARRAYLIST BY BDATE FIELD
        AList.Sort(New PersonAgeComparer())
        PEnum = AList.GetEnumerator
        While PEnum.MoveNext
            ListBox1.Items.Add(CType(PEnum.Current, Person).Name & _
                        vbTab & CType(PEnum.Current, Person).BDate)
        End While

    End Sub

    ' This Class contains a single method, which is a function that
    ' implements the IComparer interface and compares two objects of the Person type.
    ' The comparison is based on the BDate field
    ' Notice that the two objects passed to the function are casted to the Person type,
    ' to make sure that they expose a BDate field. If not, the code will fail
    Class PersonAgeComparer : Implements IComparer
        Public Function Compare(ByVal o1 As Object, _
                                  ByVal o2 As Object) As Integer _
                                  Implements IComparer.Compare
            Dim person1, person2 As Person
            Try
                person1 = CType(o1, Person)
                person2 = CType(o2, Person)
            Catch compareException As System.Exception
                Throw (compareException)
                Exit Function
            End Try
            If person1.BDate < person2.BDate Then
                Return -1
            Else
                If person1.BDate > person2.BDate Then
                    Return 1
                Else
                    Return 0
                End If
            End If
        End Function
    End Class

    ' This Class contains a single method, which is a function that
    ' implements the IComparer interface and compares two objects of the Person type.
    ' The comparison is based on the Name field
    ' Notice that the two objects passed to the function are casted to the Person type,
    ' to make sure that they expose a Name field. If not, the code will fail

    Class PersonNameComparer : Implements IComparer
        Public Function Compare(ByVal o1 As Object, _
                                  ByVal o2 As Object) As Integer _
                                  Implements IComparer.Compare
            Dim person1, person2 As Person
            Try
                person1 = CType(o1, Person)
                person2 = CType(o2, Person)
            Catch compareException As System.Exception
                Throw (compareException)
                Exit Function
            End Try
            If person1.Name < person2.Name Then
                Return -1
            Else
                If person1.Name > person2.Name Then
                    Return 1
                Else
                    Return 0
                End If
            End If
        End Function
    End Class
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -