本程式碼中使用了控制元件陣列,將三個按鈕控制元件的NAME屬性都都設定為Command1,將INDEX屬性分別設定為0,1,2.本程式碼已在VB6.0中測試透過.檔案解壓之後才能正確執行,否則不能載入到VB程式中完整程式碼如下:Dim aa() As DoublePrivate Sub Command1_Click(Index As Integer) Cls If Command1(Index).Caption = "輸入" Then ReDim aa(1 To 10) As Double "定義一個有10個元素(雙精度)的一維陣列. Text1 = "" For i = 1 To 10 aa(i) = InputBox("請輸入數字!") "用輸入框接受元素的寫入. Next For i = 1 To 10 Text1 = Text1 & aa(i) & " " Next ElseIf Command1(Index).Caption = "插入" Then Text2 = "": Text3 = "" b = InputBox("請輸入數字!") "用輸入框接受元素的寫入. Text2 = b ReDim Preserve aa(1 To 11) As Double "定義一個有11個元素(雙精度)的一維陣列. aa(11) = b For i = 11 To 2 Step -1 "處層迴圈逆序處理到第2項(再次檢查作用),處理到第2項是因為第1項沒有上浮的空間了. For j = 1 To i - 1 "內層迴圈順序處理到第10項,因為第11項沒有下沉的空間了. If aa(j) > aa(j + 1) Then tem = aa(j) aa(j) = aa(j + 1) aa(j + 1) = tem End If Next Next For i = 1 To 11 Text3 = Text3 & aa(i) & " " Next ElseIf Command1(Index).Caption = "清空" Then Text1 = "" Text2 = "" Text3 = "" End IfEnd SubPrivate Sub Form_Load() Command1(0).Caption = "輸入" Command1(1).Caption = "插入" Command1(2).Caption = "清空" End Sub
本程式碼中使用了控制元件陣列,將三個按鈕控制元件的NAME屬性都都設定為Command1,將INDEX屬性分別設定為0,1,2.本程式碼已在VB6.0中測試透過.檔案解壓之後才能正確執行,否則不能載入到VB程式中完整程式碼如下:Dim aa() As DoublePrivate Sub Command1_Click(Index As Integer) Cls If Command1(Index).Caption = "輸入" Then ReDim aa(1 To 10) As Double "定義一個有10個元素(雙精度)的一維陣列. Text1 = "" For i = 1 To 10 aa(i) = InputBox("請輸入數字!") "用輸入框接受元素的寫入. Next For i = 1 To 10 Text1 = Text1 & aa(i) & " " Next ElseIf Command1(Index).Caption = "插入" Then Text2 = "": Text3 = "" b = InputBox("請輸入數字!") "用輸入框接受元素的寫入. Text2 = b ReDim Preserve aa(1 To 11) As Double "定義一個有11個元素(雙精度)的一維陣列. aa(11) = b For i = 11 To 2 Step -1 "處層迴圈逆序處理到第2項(再次檢查作用),處理到第2項是因為第1項沒有上浮的空間了. For j = 1 To i - 1 "內層迴圈順序處理到第10項,因為第11項沒有下沉的空間了. If aa(j) > aa(j + 1) Then tem = aa(j) aa(j) = aa(j + 1) aa(j + 1) = tem End If Next Next For i = 1 To 11 Text3 = Text3 & aa(i) & " " Next ElseIf Command1(Index).Caption = "清空" Then Text1 = "" Text2 = "" Text3 = "" End IfEnd SubPrivate Sub Form_Load() Command1(0).Caption = "輸入" Command1(1).Caption = "插入" Command1(2).Caption = "清空" End Sub