This document contains code for a basic calculator application. It defines variables to store the display value, operands, and operator for calculations. Methods are included to handle button clicks for numbers, operators, clear, and equals. When a number button is clicked, the display is updated. Operator buttons set the operand and operator variables. The equals button performs the calculation and displays the result.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
66 views2 pages
' Lbldisplay - Text ""
This document contains code for a basic calculator application. It defines variables to store the display value, operands, and operator for calculations. Methods are included to handle button clicks for numbers, operators, clear, and equals. When a number button is clicked, the display is updated. Operator buttons set the operand and operator variables. The equals button performs the calculation and displays the result.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2
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