Calculator - Visual Basic 2008
Hey guys this my first TUT on VB. in this TUT i will teach you how to make a Basic calculator
This tutorial will show you how to make a basic calculator:
Open Visual Basic 2008 Express Edition. (If you don't have the program click here to download it for free.)
Click on Project next to Create:
Click on Windows Form Application, and name the project "Your name's Calculator" and then click Ok.
You can change the form text to "your name's calculator". Click on the form and then go to properties window, then change the text property.
You also need to disable the maximize control of the form. That's because you don't want the user to maximize the calculator. Go to the properties window, and change the maximize box property to False:
You also need to change the FormBorderStyle property to Fixed3D
Add a button to the form and place on top of the form:
You might be surprised of this button because we should have placed a textbox instead. Well, this is the cool thing about Visual Basic 2008, you can use a button to display text and results.
Now we have to change the button properties to look like a text box.
First we change it's text to "0.".
We align the text to be on the right. Right click on the button, click on properties. In the properties window change TextAlign to MiddleRight:
Choose the box in the middle right (the blue one in the image above)
Change the flat style of the button. In the properties window change flat style to Flat:
You also need to change the color of the button. Go to the properties window and change the backcolor property to white:
There is two more properties you need to change: TabStop to False and Enabled to False.
Your button should look like this now:
Let's add the rest of the buttons to the form.
Important: please add the button in the same order exactly as shown below.
Add four buttons. Start by adding the button on the left (1) then (2) then (3) then (4)
Change the text of the four buttons to 7 , 8, 9, / as shown above.
Add four more buttons and change their text to 4, 5, 6, *
Remember to start adding the buttons in order as the following image:
Add four more buttons in the same order as shown below and change their text to 1, 2, 3, -
Add four more buttons in the same order as shown below and change their text to 0, . , =, +
Add the last two buttons to the calculator in order as shown below and change their text to Clear, Exit:
Before we start adding the codes, we should add three labels to the form. These labels are going to be hidden beyond the calculator's borders and they will hold the numbers during the mathematical operations.
Add three labels to the form and place them under the bottom buttons:
Resize the calculator to from the bottom to hide the labels as follows:
We start adding the codes now:
Let's start programming a button at a time:
The Clear Button:
Double click on the Clear Button and add the following highlighted code:
Code:
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
Button1.Text = "0."
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub
The Exit Button:
Double click on the Exit Button and add the following highlighted code:
Code:
Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
End
End Sub
The Equal Button "=":
Double click on the equal button and add the following highlighted code:
Code:
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
If Label1.Text > "" And Label2.Text = "+" Then
Button1.Text = Val(Label1.Text) + Val(Button1.Text)
Label3.Text = Button1.Text
ElseIf Label2.Text > "" And Label2.Text = "-" Then
Button1.Text = Val(Label1.Text) - Val(Button1.Text)
Label3.Text = Button1.Text
ElseIf Label2.Text > "" And Label2.Text = "*" Then
Button1.Text = Val(Label1.Text) * Val(Button1.Text)
Label3.Text = Button1.Text
ElseIf Label2.Text > "" And Label2.Text = "/" Then
Button1.Text = Val(Label1.Text) / Val(Button1.Text)
Label3.Text = Button1.Text
Else
End If
End Sub
The Addition Button:
Double click on the plus button and add the following highlighted code:
Code:
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
Label2.Text = "+"
Label1.Text = Button1.Text
End Sub
The Subtraction Button:
Double click on the subtract button and add the following highlighted code:
Code:
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Label2.Text = "-"
Label1.Text = Button1.Text
End Sub
The Multiplication Button
Double click on the multiplication button and add the following highlighted code:
Code:
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
Label2.Text = "*"
Label1.Text = Button1.Text
End Sub
The Division Button:
Double click on the division button and add the following highlighted code:
Code:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Label2.Text = "/"
Label1.Text = Button1.Text
End Sub
The Zero Button:
Double click on the zero button and add the following highlighted code:
Code:
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
If Button1.Text = Label1.Text Then
Button1.Text = "0."
ElseIf Button1.Text = "0." Then
Button1.Text = "0."
ElseIf Button1.Text = "0" Then
Button1.Text = "0."
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "0."
Else
Button1.Text = Button1.Text & "0"
End If
End Sub
The Decimal Button:
Double click on the decimal button and add the following highlighted code:
Code:
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
If Button1.Text = "0." Then
Button1.Text = "."
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "."
ElseIf Button1.Text = Label1.Text Then
Button1.Text = "."
Else
If Button1.Tex*****ntains(".") Then
Else
Button1.Text = Button1.Text & "."
End If
End If
End Sub
The One Button:
Double click on the one button and add the following highlighted code:
Code:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
If Button1.Text = Label1.Text Then
Button1.Text = "1"
ElseIf Button1.Text = "0." Then
Button1.Text = "1"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "1"
Else
Button1.Text = Button1.Text & "1"
End If
End Sub
The Two Button:
Double click on the two button and add the following highlighted code:
Code:
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
If Button1.Text = Label1.Text Then
Button1.Text = "2"
ElseIf Button1.Text = "0." Then
Button1.Text = "2"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "2"
Else
Button1.Text = Button1.Text & "2"
End If
End Sub
The Three Button:
Double click on the three button and add the following highlighted code:
Code:
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If Button1.Text = Label1.Text Then
Button1.Text = "3"
ElseIf Button1.Text = "0." Then
Button1.Text = "3"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "3"
Else
Button1.Text = Button1.Text & "3"
End If
End Sub
The Four Button:
Double click on the four button and add the following highlighted code:
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "4"
ElseIf Button1.Text = "0." Then
Button1.Text = "4"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "4"
Else
Button1.Text = Button1.Text & "4"
End If
End Sub
the 5 button
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "5"
ElseIf Button1.Text = "0." Then
Button1.Text = "5"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "5"
Else
Button1.Text = Button1.Text & "5"
End If
End Sub
the 6 button
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "6"
ElseIf Button1.Text = "0." Then
Button1.Text = "6"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "6"
Else
Button1.Text = Button1.Text & "6"
End If
End Sub
the 7 button
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "7"
ElseIf Button1.Text = "0." Then
Button1.Text = "7"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "7"
Else
Button1.Text = Button1.Text & "7"
End If
End Sub
button 8
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "8"
ElseIf Button1.Text = "0." Then
Button1.Text = "8"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "8"
Else
Button1.Text = Button1.Text & "8"
End If
End Sub
and last but not least the 9 button
Code:
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If Button1.Text = Label1.Text Then
Button1.Text = "9"
ElseIf Button1.Text = "0." Then
Button1.Text = "9"
ElseIf Button1.Text = Label3.Text Then
Button1.Text = "9"
Else
Button1.Text = Button1.Text & "9"
End If
End Sub
WELL THAT ENDS MY SUPER LONG TUT ON HOW TO A BASIC CALCULATOR
THIS TUT IS MADE VERY NOOB FRIENDLY BUT IF YOU STILL HAVE A QUESTION THEN PLZ HESITATE TO ASK
THANK YOU!!!