How to create a basic Calculator in Visual Basics 03-20-2014, 12:46 AM
#1
Today I am going to show all you newbies to the coding world how to make a Calculator in Visual Basics, to do this tutorial I will be using Visual Studio 2013.
Alright lets get started on this. I have done what I can to make this easy to follow.
Step One
You will want to create a new project and title it what ever you want, for this I have called it "Calculator".
You will also want to make sure on Visual Studio that both Common Controls (appears on left) and Properties (appears on right) show up on the screen.
Once you start a new project this will appear in your Form (middle)
![[Image: UoROm0r.png]](http://i.imgur.com/UoROm0r.png)
Step Two
We will now add button's, textbox's and a label to the program so people know what the program is for.
Here is an example of what it should look like.
![[Image: P5khZ6H.png]](http://i.imgur.com/P5khZ6H.png)
Remember to title all of your labels, textboxes and buttons.
To name them you simply Click the thing you want to name > Go to properties > Design > (Name)
to change the display text from "label" to anything simply Click the thing you want to name > Go to properties > Apperance > Text
lbl - Label
btn - Button
txt - TextBox
Here is what I called all of mine,
Step Three
Now here we go onto the code, start by double Clicking on the button Add Then jump up to "Public Class Form1" and press enter to go onto a new line and add the following
Now go back to the part that should be named "Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click" and add the following code.
Always make sure you code is between "Private Sub" after all that text click enter and when done the code make sure "End Sub" is after your code.
Add
Once that is done you just change the + Simble on each button (subtract etc) but make sure you go back to the design (program display) and double click on each button to add it.
Subtract
Multiply
Divide
Squareroot is not as easy as the others, you can look on the web for the code but here it is anyways.
Squareroot
Here is the code for the clear button, really simpe and easy to follow, double click the button and add the following.
Step Four
If you are lazy and simply want to add the buttons and name them exactly as I did then here is the full code!
Step Five
Time to test the program to make sure there is no errors, here is a video I made of me testing it.
This program is nowhere near complete nor tested for bugs etc.
It is made to show people how easy it can be to make these programs.
Alright lets get started on this. I have done what I can to make this easy to follow.
Step One
You will want to create a new project and title it what ever you want, for this I have called it "Calculator".
You will also want to make sure on Visual Studio that both Common Controls (appears on left) and Properties (appears on right) show up on the screen.
Once you start a new project this will appear in your Form (middle)
![[Image: UoROm0r.png]](http://i.imgur.com/UoROm0r.png)
Step Two
We will now add button's, textbox's and a label to the program so people know what the program is for.
Here is an example of what it should look like.
![[Image: P5khZ6H.png]](http://i.imgur.com/P5khZ6H.png)
Remember to title all of your labels, textboxes and buttons.
To name them you simply Click the thing you want to name > Go to properties > Design > (Name)
to change the display text from "label" to anything simply Click the thing you want to name > Go to properties > Apperance > Text
lbl - Label
btn - Button
txt - TextBox
Here is what I called all of mine,
Spoiler:
Step Three
Now here we go onto the code, start by double Clicking on the button Add Then jump up to "Public Class Form1" and press enter to go onto a new line and add the following
Code:
Dim number1 As Integer
Dim number2 As Integer
Dim answer As Integer
Now go back to the part that should be named "Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click" and add the following code.
Always make sure you code is between "Private Sub" after all that text click enter and when done the code make sure "End Sub" is after your code.
Add
Code:
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 + number2
txtAnswer.Text = answer
Once that is done you just change the + Simble on each button (subtract etc) but make sure you go back to the design (program display) and double click on each button to add it.
Subtract
Code:
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 - number2
txtAnswer.Text = answer
Multiply
Code:
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 * number2
txtAnswer.Text = answer
Divide
Code:
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 / number2
txtAnswer.Text = answer
Squareroot is not as easy as the others, you can look on the web for the code but here it is anyways.
Squareroot
Code:
number1 = txtNum1.Text
answer = Math.Sqrt(number1)
txtAnswer.Text = answer
Here is the code for the clear button, really simpe and easy to follow, double click the button and add the following.
Code:
txtNum1.Clear()
txtNum2.Clear()
txtAnswer.Clear()
Step Four
If you are lazy and simply want to add the buttons and name them exactly as I did then here is the full code!
Code:
Public Class Form1
Dim number1 As Integer
Dim number2 As Integer
Dim answer As Integer
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 + number2
txtAnswer.Text = answer
End Sub
Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 - number2
txtAnswer.Text = answer
End Sub
Private Sub btnMutiply_Click(sender As Object, e As EventArgs) Handles btnMutiply.Click
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 * number2
txtAnswer.Text = answer
End Sub
Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
number1 = txtNum1.Text
number2 = txtNum2.Text
answer = number1 / number2
txtAnswer.Text = answer
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtNum1.Clear()
txtNum2.Clear()
txtAnswer.Clear()
End Sub
Private Sub btnSquareroot_Click(sender As Object, e As EventArgs) Handles btnSquareroot.Click
number1 = txtNum1.Text
answer = Math.Sqrt(number1)
txtAnswer.Text = answer
End Sub
Step Five
Time to test the program to make sure there is no errors, here is a video I made of me testing it.
This program is nowhere near complete nor tested for bugs etc.
It is made to show people how easy it can be to make these programs.