vb.net 教程 3-11 窗体控件综合编程 1

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
通过之前的学习,大家对窗体和控件知识已经有了初步的理解,本节开始将进行一些综合编程训练。

 

先从简单开始,综合编程1 计算器。

先考虑一下计算器的设计,可以参考windows的计算器。最基本那种就可以了。

下面是一个计算器控件摆放和命名:

txtValue的readonly属性设置为true;按钮的FlatStyle属性设置为Popup。

代码比较容易懂,我直接给出来:

 

    '做运算的第一个数字
    Dim numFirst As Double
    '做运算的第二个数字
    Dim numSecond As Double
    '运算符号
    Dim operatesymbol As String

    '清除
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtValue.Text = "0"
        operatesymbol = ""
    End Sub

    '删除最后一个数字
    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        If txtValue.Text.Length = 1 Then
            txtValue.Text = "0"
        ElseIf txtValue.Text.Length > 1 Then
            txtValue.Text = txtValue.Text.Substring(0, txtValue.Text.Length - 1)
        End If
    End Sub

    '按下数字按钮
    Private Sub btn1_Click(sender As Object, e As EventArgs) Handles _
            btn1.Click, btn2.Click, btn3.Click,
            btn4.Click, btn5.Click, btn6.Click,
            btn7.Click, btn8.Click, btn9.Click
        Dim btnclick As Button = CType(sender, Button)
        Dim addnumber As String = btnclick.Text
        Select Case txtValue.Text
            Case "0"
                txtValue.Text = addnumber
            Case "-0"
                txtValue.Text = "-" & addnumber
            Case Else
                txtValue.Text &= addnumber
        End Select
    End Sub

    '按下小数点
    Private Sub btnDot_Click(sender As Object, e As EventArgs) Handles btnDot.Click
        txtValue.Text &= "."
    End Sub

    '按下正负号按钮
    Private Sub btnNegPos_Click(sender As Object, e As EventArgs) Handles btnNegPos.Click
        If txtValue.Text.Substring(0, 1) = "-" Then
            txtValue.Text = txtValue.Text.Substring(1, txtValue.Text.Length - 1)
        Else
            txtValue.Text = "-" & txtValue.Text
        End If
    End Sub

    '按下运算符按钮
    Private Sub btnOperate_Click(sender As Object, e As EventArgs) Handles btnAdd.Click, btnSub.Click, btnDiv.Click, btnMul.Click
        Dim btnclick As Button = CType(sender, Button)
        numFirst = Double.Parse(txtValue.Text)
        operatesymbol = btnclick.Text
        txtValue.Text = "0"
    End Sub

    '按下等于按钮
    Private Sub btnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
        numSecond = Double.Parse(txtValue.Text)
        Dim dbValue As Double

        Select Case operatesymbol
            Case "+"
                dbValue = numFirst + numSecond
            Case "-"
                dbValue = numFirst - numSecond
            Case "*"
                dbValue = numFirst * numSecond
            Case "/"
                dbValue = numFirst / numSecond
            Case ""
                Exit Sub
            Case Else
                Exit Sub
        End Select
        Dim strValue As String = dbValue.ToString

        txtValue.Text = strValue
    End Sub

    '按下平方按钮
    Private Sub btnSquare_Click(sender As Object, e As EventArgs) Handles btnSquare.Click
        Dim btnclick As Button = CType(sender, Button)
        numFirst = Double.Parse(txtValue.Text)

        Dim dbValue As Double
        dbValue = numFirst ^ 2
        Dim strValue As String = dbValue.ToString

        txtValue.Text = strValue
    End Sub

 

 

 

 

 

 

也显而易见代码中还存在些bug,请试着改正:

1、运行完毕后,数字按钮按下将会添加到运算结果后面;

2、数字除以0或者结果超大的时候会显示“正无穷大”,任然可以参与运算,但是会导致错误;

3、多次按下运算符会导致误认为第一个做计算的数字为0,导致结果不正确。

 

学习更多vb.net知识,请参看 vb.net 教程 目录

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页