VB 6.0 講義  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

程式練習1: 乘法器

步驟1. 依畫面擺上物件 (2個 文字框, 2 個標籤, 1個命令鈕)

步驟2. 修改相關屬性

類別 預設物件名稱 屬性欄 屬性說明 屬性值
form(表單) form1 name 表單名稱 frmcalculator
    caption 表單標題 乘法器
label(標籤) label1 name 標籤名稱 lbloperator
    caption 標籤外觀 X
label(標籤) label2 name 標籤名稱 lblresult
    caption 標籤外觀 清為空白
textbox(文字框) text1 name 文字框名稱 txtno1
    text 文字框內容 清為空白
textbox(文字框) text2 name 文字框名稱 txtno2
    text 文字框內容 清為空白
CommandButton(命令鈕) command1 name 文字框名稱 cmdequal
    caption 文字框內容 =

修改完後看一下畫面, 是否有所改變

步驟3. 撰寫程式碼

在 命令鈕 cmdequal 上雙按 , 寫入以下藍色的程式碼

Private Sub cmdequal_Click()

lblresult.Caption = Val(txtno1.Text) * Val(txtno2.Text)

End Sub

解釋1: 在 cmdequal 被 Click 後, lblresult 的 caption 變為 txtno1 和 txtno2 的內容 相乘

解釋2: val( ) 為轉換數值函數