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

📄 form1.vb

📁 vb 应用实例 简单应用 ddddddddddddddddddddddddddddddddd
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form  "

    Public Sub New()
        MyBase.New()

        InitializeComponent()


    End Sub

    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

    Private components As System.ComponentModel.IContainer

    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.TextBox3 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(20, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(154, 23)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Sort"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(184, 16)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.TextBox1.Size = New System.Drawing.Size(308, 200)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(20, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(154, 23)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Reverse"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(20, 80)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(154, 21)
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = "活在当下!"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(20, 104)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(154, 23)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "Search Array"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(20, 192)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(154, 23)
        Me.Button4.TabIndex = 5
        Me.Button4.Text = "2维Array"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(20, 160)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(154, 23)
        Me.Button5.TabIndex = 6
        Me.Button5.Text = "Search Object Array"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(20, 136)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(154, 21)
        Me.TextBox3.TabIndex = 7
        Me.TextBox3.Text = "彩色照相手机"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(512, 230)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox3, Me.Button5, Me.Button4, Me.Button3, Me.TextBox2, Me.Button2, Me.TextBox1, Me.Button1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Arrays"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private ary1() As String = {"是好是坏很难说!", "活在当下!", "忘记过去的悲伤", "享受目前的生活", "Accept now, love now, enjoy now."}
    Dim ary2() As Product = {New Product(1002, "电脑"), New Product(2045, "Mouse"), New Product(3015, "键盘"), New Product(2003, "硬盘"), New Product(3001, "彩色照相手机")}
    Dim ary3(,) As String = {{"1002", "电脑"}, {"2045", "鼠标"}, {"3015", "键盘"}}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Array.Sort(ary1)
        DisplayArray(ary1, "Array Sort")

        Product.SetSortBy(Product.CompareField.Id)
        Array.Sort(ary2)
        DisplayArray(ary2, "Object Array Sort by ID")

        Product.SetSortBy(Product.CompareField.Name)
        Array.Sort(ary2)
        DisplayArray(ary2, "Object Array Sort by Name")
    End Sub


    Private Sub DisplayArray(ByVal ary As Array, ByVal dispStr As String)
        TextBox1.Text += vbCrLf & dispStr & vbCrLf
        Dim i As Integer
        For i = 0 To ary.Length - 1
            TextBox1.Text += String.Format("{0} : {1}", i, ary.GetValue(i).ToString()) & vbCrLf
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Array.Reverse(ary1)
        DisplayArray(ary1, "Array Reverse")
        Array.Reverse(ary2)
        DisplayArray(ary2, "Object Array Reverse")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
         
        Array.Sort(ary1)
        Dim pos As Integer = Array.BinarySearch(ary1, TextBox2.Text)
        
        If pos > 0 Then
            MsgBox(String.Format("Array 1 找到 {0} 位于Array位置 {1}.", TextBox2.Text, pos.ToString()))
        Else
            If pos = 0 Then
                MsgBox(String.Format("Array 1 找不到 {0} ,可能位于 {1} ({2}) 前", TextBox2.Text, pos.ToString(), ary1(1)))

            Else

                pos = -1 * pos
                Dim strArray() As String = {TextBox2.Text, (pos - 1).ToString(), pos.ToString(), ary1(pos - 1), ary1(pos)}
                MsgBox(String.Format("Array 2 找不到{0} , 可能位于 {1} ({3}) 与 {2} ({4}) 之间", strArray))
            End If
        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Text += vbCrLf
        Dim i As Integer
        Dim j As Integer
        For i = 0 To (ary3.GetLength(0) - 1)
            For j = 0 To (ary3.GetLength(1) - 1)
                TextBox1.Text += String.Format("({0}, {1}) = {2}", i, j, ary3.GetValue(i, j).ToString()) & vbCrLf
            Next j
        Next i
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
       
        Array.Sort(ary2)
        Dim c As New Product(1, TextBox3.Text)

        Dim pos As Integer = Array.BinarySearch(ary2, c, Nothing)

        
        If pos > 0 Then
            MsgBox(String.Format("Array 2 找到 {0}位于Array位置 {1}.", TextBox3.Text, pos.ToString()))
        Else
            If pos = 0 Then
                MsgBox(String.Format("Array 2 找不到 {0} ,可能位于 {1} ({2}) 前", TextBox3.Text, pos.ToString(), ary2(pos).Name))

            Else
                pos = -1 * pos
                Dim strArray() As String = {TextBox3.Text, (pos - 1).ToString(), pos.ToString(), ary2(pos - 1).Name, ary2(pos).Name}
                MsgBox(String.Format("Array 2 找不到 {0} , 可能位于 {1} ({3}) 与 {2} ({4})之间", strArray))
            End If
        End If


    End Sub
End Class

Public Class Product
    Implements IComparable

    
    Private Shared _CompareField As CompareField

    Public Id As Integer
    Public Name As String

    Public Enum CompareField
        Name
        Id
    End Enum

    Shared Sub New()
       
        _CompareField = CompareField.Name
    End Sub

   
    Public Shared Sub SetSortBy(ByVal SortKey As CompareField)
        _CompareField = SortKey
    End Sub

    Public Sub New()
        ' Set default values by delegating to the next most complex constructor
        Me.New(-1, "?")
    End Sub

    Public Sub New(ByVal Id1 As Integer, ByVal Name1 As String)
        Id = Id1
        Name = Name1
    End Sub

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        If TypeOf obj Is Product Then
            ' Create a strongly typed reference
            Dim c As Product = CType(obj, Product)
           
            If Product._CompareField = CompareField.Name Then
                If c.Name = Name Then
                    Return 0
                ElseIf c.Name < Name Then
                    Return 1
                Else
                    Return -1
                End If

               
            Else
                If c.Id = Id Then
                    Return 0
                ElseIf c.Id < Id Then
                    Return 1
                Else
                    Return -1
                End If
            End If
        Else
            Throw New ArgumentException("仅能比较 Product object. 

⌨️ 快捷键说明

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