IN VISUAL BASIC 2017 In this part of the exercise, you will create a very simple
ID: 3871239 • Letter: I
Question
IN VISUAL BASIC 2017
In this part of the exercise, you will create a very simple calculator that will perform addition, subtraction, multiplication, and division. Create buttons named btnAdd, btnSubtract, btnMultiply, and btnDivide. Create the corresponding event handlers for these buttons. Create a text box named txtInput and a label named lblResult. Initialize the Label's value to 0. In each of the button's Click event handlers, write the code to read the input from the text box and perform the corresponding numeric operation adding, subtracting multiplying or dividing the input value to the current total. Convert the input from string to a Double before performing the arithmetic. You might need to think a bit about how to keep track of the output value. The solution to the problem is a very simple one. You can declare a module-level variable.
Explanation / Answer
I will create a calculator in visual basic as follows :
I will open a Visual studio and create a new project by selecting visual basic language ,windows
I will name application as mycalculator.
Now I will add radio and text buttons using tool box.
In toolbox panel , click and drag a radio button onto form.
Copy the radio button and paste on to for, 3 more times.
Now change radio buttons text as btnAdd,btnSubtract,btnMultiply,btnDivide respectively
Now from toolbox , click and drag text box on to form. copy and paste text box , one more time on form
Now rename text boxes to txtInput and txtInput1
Now cick and drag a button control onto form , copy and paste button on to form
Rename button as calculate and clear
Now click and drag a Lable on to form , copy and paste on to form
rename it to lblSymbol for operation symbol and lblResult to store answer
Please find my code as below :
Public Class Form1
Private Sub calculate_App(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateBtn.Click
Dim number1 As Double
Dim number2 As Double
Dim answer As Double
If txtInput.Text = "" OrElse txtInput1.Text = "" Then
MessageBox.Show("Please Enter a Number")
ElseIf btnAdd.Checked = False And btnSubtract.Checked = False And btnMultiply.Checked = False And btnDivide.Checked = False Then
MessageBox.Show("Please Choose an Operation")
Else
number1 = Double.Parse(txtInput.Text)
number2 = Double.Parse(txtInput1.Text)
If btnAdd.Checked = True Then
answer = number1 + number2
lblResult.Text = answer.ToString()
ElseIf btnSubtract.Checked = True Then
answer = number1 - number2
lblResult.Text = answer.ToString()
ElseIf btnMultiply.Checked = True Then
If number1 = 0 OrElse number2 = 0 Then
lblResult.Text = "0"
Else
answer = number1 * number2
lblResult.Text = answer.ToString()
End If
ElseIf btnDivide.Checked = True Then
If number1 = 0 Then
lblResult.Text = "0"
ElseIf number2 = 0 Then
lblResult.Text = "Cannot Divide by Zero"
Else
answer = number1 / number2
lblResult.Text = answer.ToString()
End If
End If
End If
End Sub
Private Sub btnAdd_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.CheckedChanged
lblSymbol.Text = "+"
lblResult.Text = ""
End Sub
Private Sub btnSubtract_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.CheckedChanged
lblSymbol.Text = "-"
lblResult.Text = ""
End Sub
Private Sub btnMultiply_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.CheckedChanged
lblSymbol.Text = "*"
lblResult.Text = ""
End Sub
Private Sub btnDivide_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.CheckedChanged
lblSymbol.Text = "/"
lblResult.Text = ""
End Sub
Private Sub clearBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearBtn.Click
txtInput.Text = ""
txtInput1.Text = ""
lblResult.Text = ""
If btnAdd.Checked = True Then
btnAdd.Checked = False
lblSymbol.Text = ""
ElseIf btnSubtract.Checked = True Then
btnSubtract.Checked = False
lblSymbol.Text = ""
ElseIf btnMultiply.Checked = True Then
btnMultiply.Checked = False
lblSymbol.Text = ""
Else
btnDivide.Checked = False
lblSymbol.Text = ""
End If
End Sub
End Class