VB教程:命令按鈕(CommandButton)

字號:

4、命令按鈕(Command Button)        
    作用:用于開始、中斷或結束一個按鈕。
    (1)常用屬性
    Caption屬性
    Style屬性
    visible =false ' 按鈕不可見
    enabled=true ' 按鈕無效
    default =true ' 使按鈕成為缺省的“活動按鈕”,可用enter鍵選中
    cancel =true ' 使按鈕成為缺省的“取消按鈕”,可用esc鍵選中
    注意:在一個窗體中,只能有一個命令按鈕可以設為缺省的“活動按鈕”,也只能有一個命令按鈕可以設為缺省的“取消按鈕”。
    Picture '加載一幅圖片
    有時.我們可能還需要在命令按鈕上顯示某個圖案以使界面顯得活潑生動,要制作這樣的按鈕,需要設置它的兩個屬性;styLe屬性和Picture屬性。我們先將該命令按鈕的style屬性設置為1(GraPhical),再通過其Picture屬性加載一幅圖片即可,如圖顯示了一個帶圖案的命令按鈕。
    (2)主要事件
    最主要的事件是單擊(Click)事件。
    (3)常用方法
    Print方法:用于在窗體、圖片框和打印機上顯示文本。其語法格式為:
    object.Print [outputlist]
    Cls方法:用來清除運行時窗體或圖片框所生成的圖形和文本。其語法格式為:
    object.Cls
    Move方法:用于移動窗體或控件,并可以改變其尺寸大小,其語法格式為:
    object.Move left, top, width, height
    其中:
    object:可選項。表示移動窗體或控件。一個對象表達式,其值為“應用于”列表中的一個對象。如果省略object,帶有焦點的窗體缺省為 object。
    Left:必需項。指示 object 左邊的水平坐標 (x-軸)。
    Top:可選項。指示 object 頂邊的垂直坐標 (y-軸)。
    Width:可選項。指示 object 新的寬度。
    Height:可選項。指示 object 新的高度。
    (4)例子:
    見教材P109。計算器應用程序。
    代碼如下:
    Public a, b, result As Double
    Public c As Integer
    Dim op As String
    Dim index As Integer
    --------------------------------------------------------------------------------
    Private Sub Form_Load()
    a = 0
     b = 0
     Text1.Text =""
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a1_Click()
    Text1.Text = Text1.Text & a1.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a2_Click()
    Text1.Text = Text1.Text & a2.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a3_Click()
    Text1.Text = Text1.Text & a3.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a4_Click()
    Text1.Text = Text1.Text & a4.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a5_Click()
    Text1.Text = Text1.Text & a5.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a6_Click()
    Text1.Text = Text1.Text &a6.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a7_Click()
    Text1.Text = Text1.Text & a7.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a8_Click()
    Text1.Text = Text1.Text & a8.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a9_Click()
    Text1.Text = Text1.Text & a9.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a10_Click()
    Text1.Text = Text1.Text & a10.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a11_Click()
    Text1.Text =Text1.Text & a11.Caption
    End Sub
    --------------------------------------------------------------------------------
    Private Sub a12_Click()
     b = Text1.Text
     index = 0
     Select Case op
     Case +
     result = CDbl(a) + CDbl(b)
     Text1.Text = CStr(result)
     Case -
     result = CDbl(a) - CDbl(b)
     Text1.Text = CStr(result)
     Case *
     result = CDbl(a) * CDbl(b)
     Text1.Text = CStr(result)
     Case /
     If CDbl(b) = 0
     Then Text1.Text = error
     Else
     result = CDbl(a) / CDbl(b)
     Text1.Text = CStr(result)
     End If
     End Select
     If Text1.Text = error Then Text1.Text = CStr(result)
    End Sub
    --------------------------------------------------------------------------------
    Private Sub b1_Click()
     If index = 0 Then a = Text1.Text
     index = index + 1