form1.vb

来自「Visual Basic .NET程序设计教程源代码」· VB 代码 · 共 109 行

VB
109
字号
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim n As Integer                   '为窗体级变量,保存当前输入数据的个数
    '插入过程insert的形象a( )保存输入的有序数据,x为待插入的数
    Sub insert(ByVal a() As Single, ByVal x!)
        Dim i%, j%
        j = 1
        Do While j < n And x > a(j)    '查找x应插入的位置j
            j = j + 1
        Loop
        For i = n - 1 To j Step -1     'n-j个元素往右移
            a(i + 1) = a(i)
        Next i
        a(j) = x                       'x插入数组中的第i个位置
        Debug.WriteLine(x)
    End Sub

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents PictureBox2 As System.Windows.Forms.PictureBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.PictureBox2 = New System.Windows.Forms.PictureBox
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(8, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(48, 184)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'PictureBox2
        '
        Me.PictureBox2.Location = New System.Drawing.Point(72, 56)
        Me.PictureBox2.Name = "PictureBox2"
        Me.PictureBox2.Size = New System.Drawing.Size(208, 176)
        Me.PictureBox2.TabIndex = 1
        Me.PictureBox2.TabStop = False
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(176, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.PictureBox2, Me.PictureBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Static b(19) As Single
        Dim i%
        If n = 20 Then
            MsgBox("数据太多!", 1, "警告")
            End
        End If                             '当按回车,表示一个数输入
        If e.KeyChar = Chr(13) Then        '当按回车,表示一个数输入
            n = n + 1
            insert(b(), Val(TextBox1.Text))    '调用insert过程,将输入的数插入到数组中
            'PictureBox2.Print(TextBox1)    '显示刚输入的数
            'For i = 1 To n                 '显示插入后的有序数组
            'PictureBox1.Print(bb(i))
            'Next i
            'PictureBox1.Print()
            'TextBox1.Text = ""
        End If
    End Sub
End Class

⌨️ 快捷键说明

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