⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculatorform.vb

📁 Mastering VBNet Include Source Code
💻 VB
📖 第 1 页 / 共 2 页
字号:
        '
        Me.lblDisplay.BackColor = System.Drawing.Color.Green
        Me.lblDisplay.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblDisplay.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblDisplay.ForeColor = System.Drawing.Color.Lime
        Me.lblDisplay.Location = New System.Drawing.Point(8, 8)
        Me.lblDisplay.Name = "lblDisplay"
        Me.lblDisplay.Size = New System.Drawing.Size(256, 23)
        Me.lblDisplay.TabIndex = 0
        Me.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'bttnClear
        '
        Me.bttnClear.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnClear.Location = New System.Drawing.Point(88, 160)
        Me.bttnClear.Name = "bttnClear"
        Me.bttnClear.Size = New System.Drawing.Size(32, 32)
        Me.bttnClear.TabIndex = 1
        Me.bttnClear.Text = "C"
        '
        'bttnInverse
        '
        Me.bttnInverse.Font = New System.Drawing.Font("Verdana", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnInverse.Location = New System.Drawing.Point(144, 120)
        Me.bttnInverse.Name = "bttnInverse"
        Me.bttnInverse.Size = New System.Drawing.Size(32, 32)
        Me.bttnInverse.TabIndex = 1
        Me.bttnInverse.Text = "1/x"
        '
        'bttnEquals
        '
        Me.bttnEquals.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnEquals.Location = New System.Drawing.Point(144, 160)
        Me.bttnEquals.Name = "bttnEquals"
        Me.bttnEquals.Size = New System.Drawing.Size(120, 32)
        Me.bttnEquals.TabIndex = 1
        Me.bttnEquals.Text = "="
        '
        'bttnSin
        '
        Me.bttnSin.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnSin.Location = New System.Drawing.Point(224, 40)
        Me.bttnSin.Name = "bttnSin"
        Me.bttnSin.Size = New System.Drawing.Size(40, 32)
        Me.bttnSin.TabIndex = 1
        Me.bttnSin.Text = "Sin"
        '
        'bttnLog
        '
        Me.bttnLog.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnLog.Location = New System.Drawing.Point(224, 120)
        Me.bttnLog.Name = "bttnLog"
        Me.bttnLog.Size = New System.Drawing.Size(40, 32)
        Me.bttnLog.TabIndex = 1
        Me.bttnLog.Text = "Log"
        '
        'bttnCos
        '
        Me.bttnCos.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnCos.Location = New System.Drawing.Point(224, 80)
        Me.bttnCos.Name = "bttnCos"
        Me.bttnCos.Size = New System.Drawing.Size(40, 32)
        Me.bttnCos.TabIndex = 1
        Me.bttnCos.Text = "Cos"
        '
        'CalculatorForm
        '
        Me.AcceptButton = Me.bttnEquals
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.Silver
        Me.ClientSize = New System.Drawing.Size(272, 197)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnCos, Me.bttnLog, Me.bttnSin, Me.bttnNegate, Me.bttnInverse, Me.bttnDivide, Me.bttnMultiply, Me.bttnEquals, Me.bttnMinus, Me.bttnPlus, Me.bttnClear, Me.bttn0, Me.bttnPeriod, Me.bttn9, Me.bttn8, Me.bttn7, Me.bttn6, Me.bttn5, Me.bttn4, Me.bttn3, Me.bttn2, Me.bttn1, Me.lblDisplay})
        Me.ForeColor = System.Drawing.SystemColors.ActiveCaption
        Me.KeyPreview = True
        Me.Name = "CalculatorForm"
        Me.Text = "Simple Calculator"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim clearDisplay As Boolean
    Dim Operand1 As Double, Operand2 As Double
    Dim Operator As String

    Private Sub DigitClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttn0.Click, bttn1.Click, bttn2.Click, _
                                           bttn3.Click, bttn4.Click, bttn5.Click, bttn6.Click, bttn7.Click, bttn8.Click, bttn9.Click

        If clearDisplay Then
            lblDisplay.Text = ""
            clearDisplay = False
        End If
        lblDisplay.Text = lblDisplay.Text + sender.text

    End Sub

    Private Sub bttnPeriod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPeriod.Click
        If lblDisplay.Text.IndexOf(".") > 0 Then
            Exit Sub
        Else
            lblDisplay.Text = lblDisplay.Text & "."
        End If

    End Sub

    Private Sub bttnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPlus.Click
        Operand1 = Val(lblDisplay.Text)
        Operator = "+"
        clearDisplay = True
    End Sub

    Private Sub bttnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnEquals.Click
        Dim result As Double
        Operand2 = Val(lblDisplay.Text)
        Try
            Select Case Operator
                Case "+"
                    result = Operand1 + Operand2
                Case "-"
                    result = Operand1 - Operand2
                Case "*"
                    result = Operand1 * Operand2
                Case "/"
                    If Operand2 <> "0" Then
                        result = Operand1 / Operand2
                    End If
            End Select
            lblDisplay.Text = result
        Catch exc As Exception
            MsgBox(exc.Message)
            result = "ERROR"
        Finally
            lblDisplay.Text = result
            clearDisplay = True
        End Try
    End Sub

    Private Sub bttnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMinus.Click
        Operand1 = Val(lblDisplay.Text)
        Operator = "-"
        clearDisplay = True
    End Sub

    Private Sub bttnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMultiply.Click
        Operand1 = Val(lblDisplay.Text)
        Operator = "*"
        clearDisplay = True
    End Sub

    Private Sub bttnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDivide.Click
        Operand1 = Val(lblDisplay.Text)
        Operator = "/"
        clearDisplay = True
    End Sub

    Private Sub CalculatorForm_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If System.Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Then
            If clearDisplay Then
                lblDisplay.Text = ""
                clearDisplay = False
            End If
            lblDisplay.Text = lblDisplay.Text + e.KeyChar
        End If
        If e.KeyChar = "C" Or e.KeyChar = "c" Then
            lblDisplay.Text = ""
        End If
        '''  the following statements program the keys that correspond to operators
        '''  the Select Case statement is discussed in Chapter 4
        '''  when an operator key is pressed, we call the event handler for the
        '''  corresponding button, as if the user had clicked the button
        '''  since the Equals button is the default button of the form, you can
        '''  perform simple arithmetic operations without using the mouse at all.
        '''  just press the digits of the first number, then the symbol of the operation,
        '''  then the digits of the second number and then press Enter to see the result
        '''  you can explor this code, as well as program the other buttons on the form,
        '''  after you have read Chapters 4 and 5.
        Select Case e.KeyChar
            Case "+"
                bttnPlus_Click(sender, e)
            Case "-"
                bttnMinus_Click(sender, e)
            Case "*"
                bttnMultiply_Click(sender, e)
            Case "/"
                bttnDivide_Click(sender, e)
        End Select
    End Sub

    Private Sub bttnInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnInverse.Click
        If Val(lblDisplay.Text) <> 0 Then
            lblDisplay.Text = 1 / lblDisplay.Text
            clearDisplay = True
        End If
    End Sub

    Private Sub bttnNegate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnNegate.Click
        lblDisplay.Text = -Val(lblDisplay.Text)
        clearDisplay = True
    End Sub

    Private Sub bttnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnClear.Click
        lblDisplay.Text = ""
    End Sub

    Private Sub bttnSin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSin.Click
        lblDisplay.Text = Math.Sin(lblDisplay.Text)
        clearDisplay = True
    End Sub

    Private Sub bttnCos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCos.Click
        lblDisplay.Text = Math.Cos(lblDisplay.Text)
        clearDisplay = True
    End Sub

    Private Sub bttnLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnLog.Click
        If Val(lblDisplay.Text) < 0 Then
            MsgBox("Can't calculate the logarithm of a negative number")
        Else
            lblDisplay.Text = Math.Log(lblDisplay.Text)
        End If
        clearDisplay = True
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -