Sabtu, 24 September 2011

Calculator Sederhana

Public Class Form1
    Dim angkasatu As Decimal
    Dim angkadua As Decimal
    Dim hasil As Decimal
    Dim status As Integer = 1
    Dim statuskoma As Integer = 0
    Dim statusminus As Integer = 0
    Dim hitung As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "1"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "1"
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "2"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "2"
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "3"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "3"
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "4"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "4"
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "5"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "5"
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "6"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "6"
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "7"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "7"
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "8"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "8"
        End If
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "9"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "9"
        End If
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        If TextBox1.Text = "0" Or status = 0 Then
            TextBox1.Text = "0"
            status = 1
        Else
            TextBox1.Text = TextBox1.Text & "0"
        End If
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "+"
        status = 0
        statuskoma = 0
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "-"
        status = 0
        statuskoma = 0
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "*"
        status = 0
        statuskoma = 0
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "/"
        status = 0
        statuskoma = 0
    End Sub

    Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
        angkadua = Val(TextBox1.Text)
        hasil = angkasatu
        If hitung = "+" Then
            hasil = hasil + angkadua
        End If
        If hitung = "-" Then
            hasil = hasil - angkadua
        End If
        If hitung = "^" Then
            hasil = hasil * hasil
        End If
        If hitung = "√" Then
            hasil = Math.Sqrt(hasil)
        End If
        If hitung = "*" Then
            hasil = hasil * angkadua
        End If
        If hitung = "/" Then
            If angkadua = "0" Then
                MessageBox.Show("You Can't Devide by 0", "Warning")
            Else
                hasil = hasil / angkadua
            End If
        End If
        angkasatu = hasil
        TextBox1.Text = angkasatu
        status = 0
        statuskoma = 0
        statusminus = 0
    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        If angkasatu <> 0 Or angkadua <> 0 Or hasil <> 0 Then
            angkasatu = 0
            angkadua = 0
            hasil = 0
        End If
        TextBox1.Text = "0"
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If statusminus = 0 Then
            If status <> 0 And TextBox1.Text <> "0" Then
                TextBox1.Text = "-" & TextBox1.Text
                statusminus = 1
            End If
        ElseIf statusminus = 1 Then
            If status <> 0 Then
                TextBox1.Text = Microsoft.VisualBasic.Right(TextBox1.Text, Len(TextBox1.Text) - 1)
                statusminus = 0
            End If
        End If
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        If statuskoma = 0 Then
            If status <> 0 Then
                TextBox1.Text = TextBox1.Text & "."
                statuskoma = 1
            End If
        End If
    End Sub

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        If TextBox1.Text <> "0" Then
            TextBox1.Text = Microsoft.VisualBasic.Left(TextBox1.Text, Len(TextBox1.Text) - 1)
            If Len(TextBox1.Text) = 0 Then
                TextBox1.Text = "0"
            End If
        End If
    End Sub

    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "^"
        status = 0
        statuskoma = 0
    End Sub
    Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click
        If TextBox1.Text <> "0" Then
            TextBox1.Text = Microsoft.VisualBasic.Left(TextBox1.Text, Len(TextBox1.Text) - 1)
            If Len(TextBox1.Text) = 0 Then
                TextBox1.Text = "0"
            End If
        End If
    End Sub

    Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click
        angkasatu = Val(TextBox1.Text)
        hitung = "√"
        status = 0
        statuskoma = 0
    End Sub
End Class

Tidak ada komentar:

Posting Komentar