VB教程:命令按钮(CommandButton)

2026-03-23
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