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

📄 mathform.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Public Class CalculatorForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents bttn1 As System.Windows.Forms.Button
    Friend WithEvents bttn2 As System.Windows.Forms.Button
    Friend WithEvents bttn3 As System.Windows.Forms.Button
    Friend WithEvents bttn4 As System.Windows.Forms.Button
    Friend WithEvents bttn5 As System.Windows.Forms.Button
    Friend WithEvents bttn6 As System.Windows.Forms.Button
    Friend WithEvents bttn7 As System.Windows.Forms.Button
    Friend WithEvents bttn8 As System.Windows.Forms.Button
    Friend WithEvents bttn9 As System.Windows.Forms.Button
    Friend WithEvents bttn0 As System.Windows.Forms.Button
    Friend WithEvents lblDisplay As System.Windows.Forms.Label
    Friend WithEvents bttnPeriod As System.Windows.Forms.Button
    Friend WithEvents bttnPlus As System.Windows.Forms.Button
    Friend WithEvents bttnMinus As System.Windows.Forms.Button
    Friend WithEvents bttnEquals As System.Windows.Forms.Button
    Friend WithEvents bttnMultiply As System.Windows.Forms.Button
    Friend WithEvents bttnDivide As System.Windows.Forms.Button
    Friend WithEvents bttnClear As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.bttnPeriod = New System.Windows.Forms.Button()
        Me.bttnEquals = New System.Windows.Forms.Button()
        Me.bttn6 = New System.Windows.Forms.Button()
        Me.bttn7 = New System.Windows.Forms.Button()
        Me.bttn4 = New System.Windows.Forms.Button()
        Me.bttnClear = New System.Windows.Forms.Button()
        Me.bttn2 = New System.Windows.Forms.Button()
        Me.bttn3 = New System.Windows.Forms.Button()
        Me.bttn0 = New System.Windows.Forms.Button()
        Me.bttn1 = New System.Windows.Forms.Button()
        Me.bttnPlus = New System.Windows.Forms.Button()
        Me.bttn5 = New System.Windows.Forms.Button()
        Me.bttnMinus = New System.Windows.Forms.Button()
        Me.bttn8 = New System.Windows.Forms.Button()
        Me.bttn9 = New System.Windows.Forms.Button()
        Me.lblDisplay = New System.Windows.Forms.Label()
        Me.bttnMultiply = New System.Windows.Forms.Button()
        Me.bttnDivide = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'bttnPeriod
        '
        Me.bttnPeriod.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnPeriod.Location = New System.Drawing.Point(8, 160)
        Me.bttnPeriod.Name = "bttnPeriod"
        Me.bttnPeriod.Size = New System.Drawing.Size(32, 32)
        Me.bttnPeriod.TabIndex = 1
        Me.bttnPeriod.Text = "."
        '
        '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(128, 160)
        Me.bttnEquals.Name = "bttnEquals"
        Me.bttnEquals.Size = New System.Drawing.Size(80, 32)
        Me.bttnEquals.TabIndex = 1
        Me.bttnEquals.Text = "="
        '
        'bttn6
        '
        Me.bttn6.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn6.Location = New System.Drawing.Point(88, 80)
        Me.bttn6.Name = "bttn6"
        Me.bttn6.Size = New System.Drawing.Size(32, 32)
        Me.bttn6.TabIndex = 1
        Me.bttn6.Text = "6"
        '
        'bttn7
        '
        Me.bttn7.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn7.Location = New System.Drawing.Point(8, 120)
        Me.bttn7.Name = "bttn7"
        Me.bttn7.Size = New System.Drawing.Size(32, 32)
        Me.bttn7.TabIndex = 1
        Me.bttn7.Text = "7"
        '
        'bttn4
        '
        Me.bttn4.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn4.Location = New System.Drawing.Point(8, 80)
        Me.bttn4.Name = "bttn4"
        Me.bttn4.Size = New System.Drawing.Size(32, 32)
        Me.bttn4.TabIndex = 1
        Me.bttn4.Text = "4"
        '
        '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"
        '
        'bttn2
        '
        Me.bttn2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn2.Location = New System.Drawing.Point(48, 40)
        Me.bttn2.Name = "bttn2"
        Me.bttn2.Size = New System.Drawing.Size(32, 32)
        Me.bttn2.TabIndex = 1
        Me.bttn2.Text = "2"
        '
        'bttn3
        '
        Me.bttn3.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn3.Location = New System.Drawing.Point(88, 40)
        Me.bttn3.Name = "bttn3"
        Me.bttn3.Size = New System.Drawing.Size(32, 32)
        Me.bttn3.TabIndex = 1
        Me.bttn3.Text = "3"
        '
        'bttn0
        '
        Me.bttn0.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn0.Location = New System.Drawing.Point(48, 160)
        Me.bttn0.Name = "bttn0"
        Me.bttn0.Size = New System.Drawing.Size(32, 32)
        Me.bttn0.TabIndex = 1
        Me.bttn0.Text = "0"
        '
        'bttn1
        '
        Me.bttn1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn1.Location = New System.Drawing.Point(8, 40)
        Me.bttn1.Name = "bttn1"
        Me.bttn1.Size = New System.Drawing.Size(32, 32)
        Me.bttn1.TabIndex = 1
        Me.bttn1.Text = "1"
        '
        'bttnPlus
        '
        Me.bttnPlus.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnPlus.Location = New System.Drawing.Point(136, 40)
        Me.bttnPlus.Name = "bttnPlus"
        Me.bttnPlus.Size = New System.Drawing.Size(32, 32)
        Me.bttnPlus.TabIndex = 1
        Me.bttnPlus.Text = "+"
        '
        'bttn5
        '
        Me.bttn5.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn5.Location = New System.Drawing.Point(48, 80)
        Me.bttn5.Name = "bttn5"
        Me.bttn5.Size = New System.Drawing.Size(32, 32)
        Me.bttn5.TabIndex = 1
        Me.bttn5.Text = "5"
        '
        'bttnMinus
        '
        Me.bttnMinus.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnMinus.Location = New System.Drawing.Point(176, 40)
        Me.bttnMinus.Name = "bttnMinus"
        Me.bttnMinus.Size = New System.Drawing.Size(32, 32)
        Me.bttnMinus.TabIndex = 1
        Me.bttnMinus.Text = "-"
        '
        'bttn8
        '
        Me.bttn8.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn8.Location = New System.Drawing.Point(48, 120)
        Me.bttn8.Name = "bttn8"
        Me.bttn8.Size = New System.Drawing.Size(32, 32)
        Me.bttn8.TabIndex = 1
        Me.bttn8.Text = "8"
        '
        'bttn9
        '
        Me.bttn9.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttn9.Location = New System.Drawing.Point(88, 120)
        Me.bttn9.Name = "bttn9"
        Me.bttn9.Size = New System.Drawing.Size(32, 32)
        Me.bttn9.TabIndex = 1
        Me.bttn9.Text = "9"
        '
        'lblDisplay
        '
        Me.lblDisplay.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.lblDisplay.Location = New System.Drawing.Point(8, 8)
        Me.lblDisplay.Name = "lblDisplay"
        Me.lblDisplay.Size = New System.Drawing.Size(208, 23)
        Me.lblDisplay.TabIndex = 0
        '
        'bttnMultiply
        '
        Me.bttnMultiply.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnMultiply.Location = New System.Drawing.Point(136, 80)
        Me.bttnMultiply.Name = "bttnMultiply"
        Me.bttnMultiply.Size = New System.Drawing.Size(32, 32)
        Me.bttnMultiply.TabIndex = 1
        Me.bttnMultiply.Text = "*"
        '
        'bttnDivide
        '
        Me.bttnDivide.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnDivide.Location = New System.Drawing.Point(176, 80)
        Me.bttnDivide.Name = "bttnDivide"
        Me.bttnDivide.Size = New System.Drawing.Size(32, 32)
        Me.bttnDivide.TabIndex = 1
        Me.bttnDivide.Text = "/"
        '
        'CalculatorForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(216, 197)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {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.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 bttn1_Click(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 = Val(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 lblDisplay.Text = result
            End Select
        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 ' lblDisplay.Text = ""
    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
    End Sub

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

⌨️ 快捷键说明

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