Sie sind auf Seite 1von 8

Create a new project.

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, 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 FlatStyle of the button. In the properties window


change FlatStyle 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 are 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:
Page3
Go to the code page and add the following declarations:

?
1Dim value1 As String = ""
2Dim value2 As String = ""
3Dim value3 As Double = 0.0

Clear Button Code


?
1Private Sub Button18_Click(sender As System.Object, e As System.EventArgs) Handles Button18.Cl
2 value1 = ""
3 value2 = ""
4 value3 = 0.0
5 Button1.Text = 0.0
End Sub
6

Exit Button Code


?
1Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles Button19.Cl
2 End
3 End Sub

Equal Button Code


?
1 Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
2 If value1 > "" And value2 = "+" Then
3 Button1.Text = Val(value1) + Val(Button1.Text)
4 value3 = Button1.Text
5 ElseIf value2 > "" And value2 = "-" Then
Button1.Text = Val(value1) - Val(Button1.Text)
6 value3 = Button1.Text
7 ElseIf value2 > "" And value2 = "*" Then
8 Button1.Text = Val(value1) * Val(Button1.Text)
9 value3 = Button1.Text
ElseIf value2 > "" And value2 = "/" Then
10 Button1.Text = Val(value1) / Val(Button1.Text)
11 value3 = Button1.Text
12 Else
13 End If
14 End Sub
15
16

Addition, Subtraction, Multiplication and Divition Buttons


Code will be handled under one sub
?
1Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
2 value2 = sender.text
3 value1 = Button1.Text
4 End Sub

Zero Button Code


?
1
2 Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14.C
3 If Button1.Text = value1 Then
Button1.Text = "0."
4 ElseIf Button1.Text = "0." Then
5 Button1.Text = "0."
6 ElseIf Button1.Text = "0" Then
7 Button1.Text = "0."
8 ElseIf Button1.Text = value1 Then
Button1.Text = "0."
9 Else : Button1.Text = Button1.Text & "0"
10 End If
11 End Sub
12

Decimal Button Code


?
1
2 Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.C
3 If Button1.Text = "0." Then
4 Button1.Text = "."
ElseIf Button1.Text = value3 Then
5 Button1.Text = "."
6 ElseIf Button1.Text = value1 Then
7 Button1.Text = "."
8 Else
9 If Button1.Text.Contains(".") Then
Else
10 Button1.Text = Button1.Text & "."
11 End If
12 End If
13 End Sub
14
Numbers 1 - 9 Buttons Code under one sub:
?
1 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Cli
2 Button12.Click, Button11.Click, Button10.Click
3 If Button1.Text = value1 Then
4 Button1.Text = sender.text
5 ElseIf Button1.Text = "0." Then
Button1.Text = sender.text
6 ElseIf Button1.Text = value3 Then
7 Button1.Text = sender.text
8 Else
9 Button1.Text = Button1.Text & sender.text
End If
10
End Sub
11

Download Calculator Source Code

Das könnte Ihnen auch gefallen