要求把代碼輸入後能直接在VB環境中運行
在窗體中添加三個標簽,壹個按鈕,壹個框架在框架中添加壹個文本框
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 48 Or KeyCode > 57 And KeyCode < 96 Or KeyCode > 150 And KeyCode <> 109 And _
KeyCode <> 189 Then Text1 = ""
End Sub
Private Sub Form_Load()
Label1(0).Visible = False
Label1(0).Alignment = 2
Label1(0).Top = 100
Label1(0).Left = 100
Label1(0).Height = 300
Label1(0).Width = 800
Label1(0).BackColor = QBColor(15)
Label1(0).FontSize = 12
Caption = "演示“起泡法”從小到大的排序過程"
Top = 0
Left = 200
Width = 2 * Label1(0).Left + 12 * (Label1(0).Width + 100)
Height = 7000
Label2.Top = 5700
Label2.Left = 500
Label2.FontSize = 12
Label2.Width = Width - 2 * Label2.Left
Label2.WordWrap = True
Label2.AutoSize = True
a = Space(4) & "數從文本輸入,每輸完壹個數後,按回車鍵,數的個數可變.排序開始之前所有"
a = a & "所有的數都是白底黑字).單擊“開始”按鈕,開始比較(紅底)數與(黃底)數"
a = a & "若(紅底)數大就交換兩數的位置;否則,兩數仍保持原位置.位置已確定不變的數為"
Label2 = a & "(底黑字)."
Label3 = ""
Label3.FontSize = 15
Label3.Top = Label2.Top - 500
Label3.Left = 1300
Label3.AutoSize = True
Frame1.Top = Height - 2300
Frame1.Left = Width / 3
Frame1.Width = 2300
Frame1.Height = 850
Frame1.Caption = "輸入-32768到32767的數"
Text1.Top = 300
Text1.Left = 500
Text1.Height = 370
Text1.Width = 1450
Text1.FontSize = 14
Text1.MaxLength = 6
Text1 = ""
Command1.Top = Frame1.Top + 200
Command1.Left = Frame1.Left + Frame1.Width + 100
Command1.Caption = "開始排序"
Command1.Enabled = False
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static kx As Byte, ky As Byte
If KeyAscii = 13 Then
If Text1 <> "" Then
x = Val(Text1)
If x >= -32768 And x <= 32767 Then
If n = 0 Then
Label1(0) = x: Label1(0).Visible = True
Else
Load Label1(n): Label1(n) = x
Label1(n).Visible = True
kx = kx + 1
If kx Mod 12 = 0 Then ky = ky + 1: kx = 0
Label1(n).Left = Label1(0).Left + kx * (Label1(0).Width + 100)
Label1(n).Top = Label1(0).Top + ky * (Label1(0).Height + 100)
Command1.Enabled = True
End If
Text1 = "": n = n + 1
End If
End If
End If
End Sub