Saturday, April 7, 2018

Calculator

Create a new project.
You can change the form text to "your name's calculator". Click on the form and then go to propertieswindow, 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:
start a calculator tutorial in visual basic
You also need to change the FormBorderStyle property to Fixed3D
change calculator form property in vb.net
Add a button to the form and place on top of the form:
Visual Basic 2008 Calculator
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.".
add button to vb calculator
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:
Visual Basic 2008 Calculator
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:
Visual Basic 2008 Calculator
You also need to change the color of the button. Go to the properties window and change the backcolor property to white:
Visual Basic 2008 Calculator
There are two more properties you need to change: TabStop to False and Enabled to False.
Visual Basic 2008 Calculator
Your button should look like this now:
Visual Basic 2008 Calculator




Let's add the rest of the buttons to the form.
Important: please add the button in the same order exactly as shown below.
Visual Basic 2010 Calculator
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:
Visual Basic 2008 Calculator
Add four more buttons in the same order as shown below and change their text to 1, 2, 3, -
Visual Basic 2010 Calculator
Add four more buttons in the same order as shown below and change their text to 0, . , =, +
Visual Basic 2013 Calculator
Add the last two buttons to the calculator in order as shown below and change their text to Clear, Exit:
Visual Basic .NET Calculator


Go to the code page and add the following declarations:
Dim value1 As String = ""
Dim value2 As String = ""
Dim value3 As Double = 0.0

Clear Button Code

Private Sub Button18_Click(sender As System.Object, e As System.EventArgs) Handles Button18.Click
        value1 = ""
        value2 = ""
        value3 = 0.0
        Button1.Text = 0.0
    End Sub

Exit Button Code

Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles Button19.Click
        End
End Sub

Equal Button Code

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        If value1 > "" And value2 = "+" Then
            Button1.Text = Val(value1) + Val(Button1.Text)
            value3 = Button1.Text
        ElseIf value2 > "" And value2 = "-" Then
            Button1.Text = Val(value1) - Val(Button1.Text)
            value3 = Button1.Text
        ElseIf value2 > "" And value2 = "*" Then
            Button1.Text = Val(value1) * Val(Button1.Text)
            value3 = Button1.Text
        ElseIf value2 > "" And value2 = "/" Then
            Button1.Text = Val(value1) / Val(Button1.Text)
            value3 = Button1.Text
        Else
        End If
    End Sub

Addition, Subtraction, Multiplication and Divition Buttons Code will be handled under one sub

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click, Button13.Click, Button9.Click, Button5.Click
        value2 = sender.text
        value1 = Button1.Text
    End Sub

Zero Button Code

Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14.Click
        If Button1.Text = value1 Then
            Button1.Text = "0."
        ElseIf Button1.Text = "0." Then
            Button1.Text = "0."
        ElseIf Button1.Text = "0" Then
            Button1.Text = "0."
        ElseIf Button1.Text = value1 Then
            Button1.Text = "0."
        Else : Button1.Text = Button1.Text & "0"
        End If
    End Sub

Decimal Button Code

 Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click
        If Button1.Text = "0." Then
            Button1.Text = "."
        ElseIf Button1.Text = value3 Then
            Button1.Text = "."
        ElseIf Button1.Text = value1 Then
            Button1.Text = "."
        Else
            If Button1.Text.Contains(".") Then
            Else
                Button1.Text = Button1.Text & "."
            End If
        End If
    End Sub

Numbers 1 - 9 Buttons Code under one sub:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click, Button7.Click, Button6.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click
        If Button1.Text = value1 Then
            Button1.Text = sender.text
        ElseIf Button1.Text = "0." Then
            Button1.Text = sender.text
        ElseIf Button1.Text = value3 Then
            Button1.Text = sender.text
        Else
            Button1.Text = Button1.Text & sender.text
        End If
    End Sub

No comments:

Computer Terms

The termination of the process due to a program or system fault      -      Abend(abnormal ending) To terminate a process before completion....