Creating a Calculator | VB.Net Part2

Okay, so basically we’re now at the point where we have our PseudoCode, and should have a pretty good footing for where we have to go. Looking at the PseudoCode once again, we see some things that we know we’re going to need that wasn’t stated by a control. For instance: numFirst, numSec, and operator are all things that aren’t related to controls but hold value for use. These are going to be variables that we use in our project to store what’s going on. For this tutorial, I’m going to set these datatypes as Strings, though numFirst and numSec should be doubles (10.00 1.00 4.50, etc). Like I said, this is a Beginner’s lesson, I’ll tackle datatypes at a later time.

So lets look at the logic one more time just to get an understanding.

  • numFirst and numSec are going to hold our numbers. 
  • operator will hold what the operator will be. 
  • When the +-/* buttons are clicked, they check to see if numSec was previously set. 
  • If numSec was NOT set, then we know that we’re dealing with the first operation.
  • If numSec WAS set, then we need to first calculate what numFirst and numSec equal, this depends on the Operator previously set, set numFirst to equal that result, and finally get ready to set numSec with the next number inputted.
  • If the clear button is clicked (remember i stated this is a ClearEverything, not just clear the current output) all variables will be cleared.

Now lets design our form.
At the current time I will NOT be including screenshots, but later on I will. Sorry.
Okay, so in Design mode, go ahead and draw your buttons.

  • Throw on your 10 buttons for numbers. For my final code, I actually left the Button names for these ten as default Button1, Button2, etc. 
  • Throw on your 5 Operators and 1 clear button. I named mine btnClear, btnAdd, btnMinus, btnDivide, btnMultiply
  • Add your textbox for output. txtOutput seemed like a good name.

Now you should have a pretty good look calculator. Only thing is NOTHING works. So why don’t we start our coding, and convert our PseudoCode into VB.Net code.
First lets create our variables. Do this just after the Class Form so that it’s global
dim num1, num2, oper as string

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
End Sub

You’re going to take notice to the AppendText instead of the = text. This is because we dont want to REPLACE the value of the textbox with this new value. We want to add on to the number if one was already set. We call this a logical error if you leave it as =, because though this is an error, it’s not producing the result we thought it’d produce, it’s not actually doing anything wrong. We’re the ones at fault, not the code or compiler.

Okay, Now with JUST that code, we can easily see that we can click numbers all day and the Textbox will show them. Press 1, output will say “1”. Press 4 then press 6 then 9 the output will say “469”. This is good now lets code an Operator button

Add and Equals
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
num1 = TextBox1.Text
oper = “+”
End Sub
Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
If oper = “” Then
MsgBox(“You must enter a second number”)
Exit Sub
End If
num2 = TextBox1.Text
Select Case oper
Case “+”
TextBox1.Text = num1 + num2
End Select
End Sub

If you do this for the other buttons you should have yourself a simple calculator that can only do 1 set of operations. This is what you’ll find using those youtube videos that I described in Part 1.
As you see in the Equals subroutine, we have a Select Case instead of If Thens. This is better and faster since with If thens, all possibilities have to be tested to find what’s true, where as this one just looks for what Case is being used and executes.

If you went ahead and created the other functions you should have
Select Case oper
Case “+”
Return num1 + num2
Case “-”
Return num1 – num2
Case “*”
Return num1 * num2
Case “/”
Return num1 / num2
End Select

If we think back to the PseudoCode we’re missing some things from Operator buttons. Let me repost it to get a quick look again.
if numSec isEmpty then
Set operator to “+” or “-” or “/” or “*”
Clear txtOutput
numFirst = numFirst operator numSec
Set operator ”+” or “-” or “/” or “*”
Clear txtOutput

So looking at this we need to make some small changes to our btnAdd/Minus/Divide/Multiply subroutines.

if num2 = “” then
oper = “+”
Select Case oper
Case “+”
num1 = num1 + num2
Case “-”
num1 = num1 – num2
Case “*”
num1 = num1 * num2
Case “/”
num1 = num1 / num2
End Select
oper = “+”
End If

That’s the layout for btnAdd. Now you could add that to every Operator button.
Lastly add the subroutine for btnClear which is simply to do the following
num1 = “”
num2 = “”

Now you’re just about done, but should have a working calculator that allows for continuous input.
Part1  Part3

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.