Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average
Thread Closed 


Tutorial How to create a basic Calculator in Visual Basics filter_list
Author
Message
How to create a basic Calculator in Visual Basics #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]

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]

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:
Calculator - Label
Name: lblTitle

Enter First Number - Label
Name: lblFirst

Enter Second Number - Label
Name: lblSecond

To the right of Enter First Number
TextBox
Name: txtNum1

To the right of Enter Second Number
TextBox
Name: txtNum2

Add - Button
Name: btnAdd

Subtract - Button
Name: btnSubtract

Multiply - Button
Name: btnMultiply

Divide - Button
Name: btnDivide

Squareroot - Button
Name: btnSquareroot

The Answer Is - Label
Name: lblTotal

To the right of The Answer Is
TextBox
Name: txtAnswer

Clear - Button
Name: btnClear

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.


RE: How to create a basic Calculator in Visual Basics #2
A piece of VB.NET code from a beginner programmer trying to teach implicit typecasting as a good thing for a calculator and with no exception handling...


RE: How to create a basic Calculator in Visual Basics #3
Try enabling Option Strict with this code.

For future reference, check out the following MSDN page:
Double.TryParse() -> If you want a calculator with decimals.

You should really be using Doubles (or any data type that supports decimals) for this, namely for the Sqrt() and division functions which won't always return a whole integer.


RE: How to create a basic Calculator in Visual Basics #4
I would recommend Decimal instead for it's greater floating point precision over Double or Float.


RE: How to create a basic Calculator in Visual Basics #5
(03-22-2014, 07:59 PM)0xDEAD10CC Wrote: I would recommend Decimal instead for it's greater floating point precision over Double or Float.

That's true. It does have a smaller range (±5.0 x 10^-324 to ±1.7 x 10^308 for Double, ±1.0 x 10^-28 to ±7.9 x 10^28 for Decimal) though. If this calculator is meant to function with higher values it might be more ideal to use a double. If not, a Decimal would be more beneficial since float/double are approximations, not exact values.

Decimal.TryParse() MSDN for OP


RE: How to create a basic Calculator in Visual Basics #6
Nice tutorial, but I prefer Python. I'm familiarising myself with TKinter.


RE: How to create a basic Calculator in Visual Basics #7
(03-22-2014, 08:16 PM).Shebang Wrote: That's true. It does have a smaller range (±5.0 x 10^-324 to ±1.7 x 10^308 for Double, ±1.0 x 10^-28 to ±7.9 x 10^28 for Decimal) though. If this calculator is meant to function with higher values it might be more ideal to use a double. If not, a Decimal would be more beneficial since float/double are approximations, not exact values.

Decimal.TryParse() MSDN for OP

Decimal has more bits to store the number in for a more accurate representation. Higher values should not sacrifice precision for things like currency, calculators, and anything else that may need accuracy. Double and Single can still easily fail with smaller numbers too http://msdn.microsoft.com/en-us/library/xtba3z33.aspx. It's up to him though whether he would like such precision, but if it were my calculator, I'd choose it. System.Decimal can also encode trailing 0's.


RE: How to create a basic Calculator in Visual Basics #8
(03-20-2014, 11:17 PM)Apraxiahs Wrote: ...
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.
...
It is made to show people how easy it can be to make these programs.

I don't think many people are going to learn much from this tutorial. From my perspective, it just kind of looks like you're slapping together code. It works of course, but you need to tell people why it works and how you're doing it. (changing the properties to refer to them in the code, Declaring the variables, etc.)

Also, "simble".


RE: How to create a basic Calculator in Visual Basics #9
Create a calculator that will take input in infix notation and solve it.


RE: How to create a basic Calculator in Visual Basics #10
Apraxiahs Wrote: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.

You are aware this is not meant to be ran as an actual program right?
Just to show people that VB is easy to use.








Users browsing this thread: 1 Guest(s)