How to
Create Simple Calculator
1st Step: Create Calculator Design.
Ø
Drag a 3 label onto the form and change the
label TEXT in properties.
o
For 1st label change the TEXT into “First Number :”
o
For 2nd label change the TEXT into
“Second Number :”
o
For 3rd label change the TEXT into
“Result :”
Ø
Drag a 3 textbox onto the form and change the
textbox NAME in properties.
o
For 1st textbox change the NAME into “txtFirstNumber”
o
For 2nd textbox change the NAME into “txtSecondNumber”
o
For 3rd textbox change the NAME into “txtResult”
Ø
Drag a 5 buttons onto the form and change the
Button NAME and TEXT in properties.
o
For Button TEXT
§
For 1st Button Change the TEXT into
“ADD”.
§
For 2nd Button Change the TEXT into
“SUBTRACT”.
§
For 3rd Button Change the TEXT into
“MULTIPLY”.
§
For 4th Button Change the TEXT into
“DIVIDE”.
§
For 5th Button Change the TEXT into
“CLEAR”.
o
For Button NAME
§
For 1st Button Change the NAME into
“btnAdd”.
§
For 2nd Button Change the NAME into
“btnSubtract”.
§
For 3rd Button Change the NAME into
“btnMultiply”.
§
For 4th Button Change the NAME into
“btnDivide”.
§
For 5th Button Change the NAME into
“btnClear”.
2nd Step: Start Coding.
Ø
To Start Coding, double click the Form.
Ø
Declare the Variables.
o
Dim dgFirstNum,dgSecondNum,dgResult as double.
§
dgFirstNum is a value entered in the "txtFirstNumber".
§
dgSecond
is a value entered in the "txtSecondNumber".
§
dgResult is the result.
Ø
Go back to design then double click ADD button.
o
Code for the ADD button.
Private Sub btnAdd_Click()Handles
btnAdd.Click
dgFirstNum
= Val(txtFirstNumber.Text)
dgSecondNum
= Val(txtSecondNumber.Text)
dgResult =
dgFirstNum + dgSecondNum
End Sub
Ø
Go back to design then double click SUBTRACT
button.
o
Code for the SUBTRACT button.
Private Sub btnSubtract_Click()Handles
btnSubtract.Click
dgFirstNum
= Val(txtFirstNumber.Text)
dgSecondNum
= Val(txtSecondNumber.Text)
dgResult =
dgFirstNum - dgSecondNum
End Sub
Ø
Go back to design then double click MULTIPLY
button.
o
Code for the MULTIPLY button.
Private Sub btnMultiply_Click()Handles
btnMultiply.Click
dgFirstNum =
Val(txtFirstNumber.Text)
dgSecondNum
= Val(txtSecondNumber.Text)
dgResult =
dgFirstNum * dgSecondNum
End Sub
Ø
Go back to design then double click DIVIDE
button.
o
Code for the DIVIDE button.
Private Sub btnDivide_Click()Handles
btnDivide.Click
dgFirstNum = Val(txtFirstNumber.Text)
dgSecondNum
= Val(txtSecondNumber.Text)
dgResult =
dgFirstNum / dgSecondNum
End Sub
Ø
Go back to design then double click CLEAR
button.
o
Code for the CLEAR button.
Private Sub btnClear_Click()Handles
btnClear.Click
txtFirstNumber.Clear()
txtSecondNumber.Clear()
txtResult.Clear()
End Sub
3rd Step: Run the Program.
Ø
Click to start Debugging Button (F5).
Source Code:
Public Class Form1
Dim
dgFirstNum, dgSecondNum, dgResult As Double
Private Sub btnAdd_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
dgFirstNum = Val(txtFirstNumber.Text)
dgSecondNum = Val(txtSecondNumber.Text)
dgResult = dgFirstNum + dgSecondNum
txtResult.Text = dgResult
End Sub
Private Sub btnSubtract_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnSubtract.Click
dgFirstNum = Val(txtFirstNumber.Text)
dgSecondNum = Val(txtSecondNumber.Text)
dgResult = dgFirstNum - dgSecondNum
txtResult.Text = dgResult
End Sub
Private Sub btnMultiply_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnMultiply.Click
dgFirstNum = Val(txtFirstNumber.Text)
dgSecondNum = Val(txtSecondNumber.Text)
dgResult = dgFirstNum * dgSecondNum
txtResult.Text = dgResult
End Sub
Private Sub btnDivide_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnDivide.Click
dgFirstNum = Val(txtFirstNumber.Text)
dgSecondNum = Val(txtSecondNumber.Text)
dgResult = dgFirstNum / dgSecondNum
txtResult.Text = dgResult
End Sub
Private Sub btnClear_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles btnClear.Click
txtFirstNumber.Clear()
txtSecondNumber.Clear()
txtResult.Clear()
End Sub
End Class
No comments:
Post a Comment