您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> VB >> 辅导 >> 正文    
  VB教程:命令按钮(CommandButton) 【注册男护士专用博客】          

VB教程:命令按钮(CommandButton)

www.nanhushi.com     佚名   不详 

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


    If index >= 2 Then
        b = Text1.Text
    Select Case op
        Case +
            result = CDbl(a) + CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b = ""
            Text1.Text =""
        Case -
            result = CDbl(a) - CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b = ""
        Case *
            result = CDbl(a) * CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b = ""
        Case /
            If CDbl(b) = 0
                Then Text1.Text = error
            Else
                result = CDbl(a) / CDbl(b)
                Text1.Text = CStr(result)
                a = Text1.Text
                b =""
            End If
        End Select
        End If
            Text1.Text =""
            If index >= 2 Then Text1.Text = CStr(result)
            op = +  
End Sub


Private Sub b2_Click()
    If index = 0 Then a = Text1.Text
        index = index + 1
    If index >= 2 Then
        b = Text1.Text
    Select Case op
        Case +
            result = CDbl(a) + CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
            Text1.Text =""
        Case -
            result = CDbl(a) - CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b = ""
        Case *
            result = CDbl(a) * CDbl(b)
            Text1.Text = CStr(result)
            a =Text1.Text


            b =""
        Case /
            If CDbl(b) = 0 Then
                Text1.Text = error
            Else
                result = CDbl(a) / CDbl(b)
                Text1.Text = CStr(result)
                a = Text1.Text
                b = ""
            End If
    End Select
    End If
            Text1.Text = ""
            If index >= 2 Then  Text1.Text = CStr(result)  
            op = -

End Sub


Private Sub b3_Click()

  If index = 0 Then a = Text1.Text
        index = index + 1
    If index >= 2 Then
        b = Text1.Text
    Select Case op
        Case +
            result = CDbl(a) + CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
            Text1.Text =""
        Case -
            result = CDbl(a) - CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
        Case *
            result = CDbl(a) * CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
        Case /
            If CDbl(b) = 0 Then
                Text1.Text = error
            Else
                result = CDbl(a) / CDbl(b)
                Text1.Text = CStr(result)
                a = Text1.Text
                b =""
            End If
    End Select
    End If
        Text1.Text =""
        If index >= 2 Then Text1.Text = CStr(result)
        op = *

End Sub


Private Sub b4_Click()

  If index = 0 Then a = Text1.Text
        index = index + 1
    If index >= 2 Then
        b = Text1.Text Select


    select Case op
        Case +
            result = CDbl(a) + CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
            Text1.Text =
        Case -
            result = CDbl(a) - CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
        Case *
            result = CDbl(a) * CDbl(b)
            Text1.Text = CStr(result)
            a = Text1.Text
            b =""
        Case /
            If CDbl(b) = 0 Then
                Text1.Text = error
            Else
                result = CDbl(a) / CDbl(b)
                Text1.Text = CStr(result)
                a = Text1.Text
                b = ""
            End If
    End Select
    End If
        Text1.Text = ""
        If index >= 2 Then Text1.Text = CStr(result)
        op = /

End Sub

[程序演示]      [程序下载]

关于If条件语句

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。