Rabu, 13 Juli 2016

Cara membuat miniKalkulator di VB Net


Selamat malam agan-agan, kali ane dengan senang hati akan share cara buat kalkulator sederhana di VB Net. Langsung aja ane bagi source kodenya


Public Class frCalculator

    Friend Hasil As Single
    Dim operasi As String = ""
    Dim operasiHit As Boolean = False
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        operasiHit = False
        operasi = ""
        Hasil = 0
        txLayar.Text = "0"
    End Sub

    Private Sub btClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btClear.Click
        txLayar.Text = "0"
        operasi = ""
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTujuh.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "7"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "7"
        End If
    End Sub

    Private Sub btSatu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSatu.Click

        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "1"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "1"
        End If
    End Sub

    Private Sub btDua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDua.Click

        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "2"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "2"
        End If
    End Sub

    Private Sub btTiga_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTiga.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "3"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "3"
        End If
    End Sub

    Private Sub btEmpat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEmpat.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "4"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "4"
        End If
    End Sub

    Private Sub btLima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btLima.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "5"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "5"

        End If
    End Sub

    Private Sub btEnam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btEnam.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "6"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "6"
        End If
    End Sub

    Private Sub btDelapan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDelapan.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "8"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "8"

        End If
    End Sub

    Private Sub btSembilan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSembilan.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "9"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "9"
        End If
    End Sub

    Private Sub btNol_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNol.Click
        If operasiHit = True Or txLayar.Text = "0" Then
            txLayar.Text = "0"
            operasiHit = False
        Else
            txLayar.Text = txLayar.Text + "0"
        End If
    End Sub

    Private Sub btTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTambah.Click
        operasi = "tambah"
        Hasil = Val(txLayar.Text)
        operasiHit = True
    End Sub

    Private Sub btKurang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btKurang.Click
        operasi = "kurang"
        Hasil = Val(txLayar.Text)
        operasiHit = True
    End Sub

    Private Sub btKali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btKali.Click
        operasi = "kali"
        Hasil = Val(txLayar.Text)
        operasiHit = True
    End Sub

    Private Sub btBagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBagi.Click
        operasi = "bagi"
        Hasil = Val(txLayar.Text)
        operasiHit = True
    End Sub

    Private Sub btSamaDengan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSamaDengan.Click
        Select Case operasi
            Case "kali"
                Hasil = Hasil * Val(txLayar.Text)
            Case "bagi"
                Hasil = Hasil / Val(txLayar.Text)
            Case "tambah"
                Hasil = Hasil + Val(txLayar.Text)
            Case "kurang"
                Hasil = Hasil - Val(txLayar.Text)

        End Select
        txLayar.Text = Hasil
        operasiHit = True
    End Sub

    Private Sub btSin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSin.Click
        Dim phi As Double = Math.PI
        Hasil = Math.Sin((Val(txLayar.Text) / 180) * phi)

        txLayar.Text = Hasil
        operasiHit = True
    End Sub

    Private Sub btCos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCos.Click
        Dim phi As Double = Math.PI
        Hasil = Math.Cos((Val(txLayar.Text) / 180) * phi)

        txLayar.Text = Hasil
        operasiHit = True
    End Sub

    Private Sub btTan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTan.Click
        Dim phi As Double = Math.PI
        Hasil = Math.Tan((Val(txLayar.Text) / 180) * phi)

        txLayar.Text = Hasil
        operasiHit = True
    End Sub

    Private Sub btLog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btLog.Click
        Hasil = Math.Log(Val(txLayar.Text))
        txLayar.Text = Hasil
        operasiHit = True
    End Sub

    Private Sub btLn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btLn.Click
        Hasil = Math.Log10(Val(txLayar.Text))
        txLayar.Text = Hasil
        operasiHit = True
    End Sub
End Class

Catatan :
Untuk Button1_Click itu untuk input angka 7, dan untuk input angka 1 sampai 9 tetap pakai variabel btSatu dst.
Semoga bermanfaat :)

0 komentar:

Posting Komentar