File - Visual Basic 2010 Express Tutorials

advertisement
This tutorial will show you how to make a basic calculator:
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 2010, 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:
Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton18.Cli
ck
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:
Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton19.Cli
ck
End
End Sub
The Equal Button "=":
Double click on the equal button and add the following highlighted code:
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton16.Cli
ck
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:
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton17.Cli
ck
Label2.Text = "+"
Label1.Text = Button1.Text
End Sub
The Subtraction Button:
Double click on the subtract button and add the following highlighted code:
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton13.Cli
ck
Label2.Text = "-"
Label1.Text = Button1.Text
End Sub
The Multiplication Button
Double click on the multiplication button and add the following highlighted code:
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton17.Cli
ck
Label2.Text = "*"
Label1.Text = Button1.Text
End Sub
The Division Button:
Double click on the division button and add the following highlighted code:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton5.Click
Label2.Text = "/"
Label1.Text = Button1.Text
End Sub
The Zero Button:
Double click on the zero button and add the following highlighted code:
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Cli
ck
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:
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Cli
ck
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.Text.Contains(".") 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:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Cli
ck
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:
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Cli
ck
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:
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Cli
ck
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:
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 Five Button:
Double click on the five button and add the following highlighted code:
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton7.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 Six Button:
Double click on the six button and add the following highlighted code:
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton8.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 Seven Button:
Double click on the seven button and add the following highlighted code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton2.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
The Eight Button:
Double click on the eight button and add the following highlighted code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton3.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
The Nine Button:
Double click on the nine button and add the following highlighted code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesButton4.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
The calculator is ready now and should work fine.
If your calculator is working fine, you can publish it:
Download