Tuesday, December 3, 2019

Visual Basic .net Simple Standard Calculator

Step 1: Create New Project with Visual Studio (I am using 2008)
Step 2: Select Windows on Project Types
Step 3: Select Windows Forms Application as Templates
Step 4: Design your simple calculator

Note: The display used is a Label and it has two labels the lower and the upper part namely
        Upper Part:  Secondary Display
        Lower Part: Primary Display

Step 5: Double click on your form and add this codes


Step 6: Add codes to each buttons afterwards

MC

memory = 0
MemoryIndicator.Visible = False

MR

PrimaryDisplay.Text = memory
MemoryIndicator.Visible = True

MS

memory = PrimaryDisplay.Text
MemoryIndicator.Visible = True

M+

memory += CDbl(PrimaryDisplay.Text)
MemoryIndicator.Visible = True

M-

memory -= CDbl(PrimaryDisplay.Text)
MemoryIndicator.Visible = True

<-- (Backspace)

If Len(PrimaryDisplay.Text) > 0 Then
       PrimaryDisplay.Text = Mid(PrimaryDisplay.Text, 1, Len(PrimaryDisplay.Text) - 1)
End If

CE (Clear Equation)

n2 = Nothing
ans = Nothing
operator_active = Nothing
selected_operator = Nothing
SecondaryDisplay.Text = ""

C (Clear)

n1 = Nothing
n2 = Nothing
ans = Nothing
operator_active = Nothing
selected_operator = Nothing
PrimaryDisplay.Text = ""
SecondaryDisplay.Text = ""

+- (Negative Positive)

If Mid(PrimaryDisplay.Text, 1, 1) = "-" Then
            PrimaryDisplay.Text = Mid(PrimaryDisplay.Text, 2, Len(PrimaryDisplay.Text))
Else
            PrimaryDisplay.Text = "-" & PrimaryDisplay.Text
End If

SQRT (Square root)

SecondaryDisplay.Text = "Sqrt(" & PrimaryDisplay.Text & ")"
PrimaryDisplay.Text = CDbl(PrimaryDisplay.Text) ^ 0.5

% (Percentage)

percentage_active = True
SecondaryDisplay.Text = "Percentage(" & selected_operator & ")"

1/x (Reciprocal)

PrimaryDisplay.Text = 1 / CDbl(PrimaryDisplay.Text)
SecondaryDisplay.Text = "(Reciprocal)"

/ (Division)

n1 = CDbl(PrimaryDisplay.Text)
operator_active = True
selected_operator = "/"
SecondaryDisplay.Text = PrimaryDisplay.Text & "/"

* (Multiplication)

n1 = CDbl(PrimaryDisplay.Text)
operator_active = True
selected_operator = "*"
SecondaryDisplay.Text = PrimaryDisplay.Text & "*"

- (Subtraction)

n1 = CDbl(PrimaryDisplay.Text)
operator_active = True
selected_operator = "-"
SecondaryDisplay.Text = PrimaryDisplay.Text & "-"

+ (Addition)

n1 = CDbl(PrimaryDisplay.Text)
operator_active = True
selected_operator = "+"
SecondaryDisplay.Text = PrimaryDisplay.Text & "+"

= (Equal)

n2 = CDbl(PrimaryDisplay.Text)
SecondaryDisplay.Text = SecondaryDisplay.Text & PrimaryDisplay.Text
Select Case selected_operator
       Case "+"
                If percentage_active = False Then
                    ans = n1 + n2
                Else
                    ans = n1 + ((n2 / 100) * n1)
                End If
       Case "-"
                If percentage_active = False Then
                    ans = n1 - n2
                Else
                    ans = n1 - ((n2 / 100) * n1)
                End If
       Case "*"
                If percentage_active = False Then
                    ans = n1 * n2
                Else
                    ans = n1 * ((n2 / 100) * n1)
                End If
       Case "/"
                If percentage_active = False Then
                    ans = n1 / n2
                Else
                    ans = n1 / ((n2 / 100) * n1)
                End If
End Select
PrimaryDisplay.Text = ans
percentage_active = False

Decemal Point

If PrimaryDisplay.Text.Contains(".") = False Then
         PrimaryDisplay.Text = PrimaryDisplay.Text & "."
End If

Number 0 to 9

If operator_active = False Then
            PrimaryDisplay.Text = PrimaryDisplay.Text & "0"
Else
            PrimaryDisplay.Text = "0"
            operator_active = False
End If

Note: Add this codes from number 0 up to number 9 just change the number for each button

No comments:

Post a Comment

A REVIEW ON CONNIE DABATE’S MURDER CASE: Fitbit One Wearable

T he Author   ROSITO D. ORQUESTA MSIT Student at Jose Rizal Memorial State University-Dapitan Campus OIC-ICT Dean, Eastern Mindanao College ...