Dot Net Technology En. No.
- 22BECE30224
Practical : 1
Aim : Write a program for Arithmetic Calculator using Windows Application.
Steps :
=> Define the Form1 class and declare variables.
=> Each number button (0-9) has an event handler that appends the button's text to the text box. Clarity
updates the display with the clicked number.
=> Each operator button (+, -, x, /) stores the first operand (a) and the operator (op), then clears the
display for the second operand (b) and Each Operand enter then clears the display.
=> Retrieve the second operand, calculate based on the operator, and display the result.
=> The result (ans) is stored and displayed.
=> Clear the display when the clear button is clicked.
newCalculator.vb :
Public Class frmCalc
Dim a As Integer
Dim b As Integer
Dim ans As Integer
Dim op As Char
Dim newAns As Integer
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
txtDisplay.Text = txtDisplay.Text + btn1.Text
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
Dot Net Technology En. No. - 22BECE30224
txtDisplay.Text = txtDisplay.Text + btn2.Text()
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
txtDisplay.Text = txtDisplay.Text + btn3.Text
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
txtDisplay.Text = txtDisplay.Text + btn4.Text
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
txtDisplay.Text = txtDisplay.Text + btn5.Text
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
txtDisplay.Text = txtDisplay.Text + btn6.Text
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
txtDisplay.Text = txtDisplay.Text + btn7.Text
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
txtDisplay.Text = txtDisplay.Text + btn8.Text
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
Dot Net Technology En. No. - 22BECE30224
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
txtDisplay.Text = txtDisplay.Text + btn9.Text
End Sub
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtDisplay.Text = txtDisplay.Text + btn0.Text
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnAdd.Click
op = btnAdd.Text
a = Convert.ToInt16(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub btnSub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnSub.Click
op = btnSub.Text
a = Convert.ToInt16(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub btnMul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnMul.Click
op = btnMul.Text
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
Dot Net Technology En. No. - 22BECE30224
a = Convert.ToInt16(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnDiv.Click
op = btnDiv.Text
a = Convert.ToInt16(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnClear.Click
txtDisplay.Clear()
End Sub
Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
btnEqual.Click
b = Convert.ToInt16(txtDisplay.Text)
If (op = "+") Then
ans = a + b
End If
If (op = "-") Then
ans = a - b
End If
If (op = "X") Then
ans = a * b
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
Dot Net Technology En. No. - 22BECE30224
End If
If (op = "/") Then
ans = a / b
End If
txtDisplay.Text = ans.ToString()
newAns = Convert.ToInt16(txtDisplay.Text)
End Sub
End Class
Output :
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH
Dot Net Technology En. No. - 22BECE30224
LDRP INSTITUTE OF TECHNOLOGY AND RESEARCH