Program Calculator Pada Visual Basic

Sunday, 13 October 20130 comments


Begitu mudahnya membuat program Calculator pada Visual Basic. Mau tahu programnya, lihat saja perintah di bawah ini :

Designlah form program calculator pada visual basic terlebih dahulu

Program Calculator Pada Visual Basic
















Untuk penamaan komponen lihat tabel di bawah ini :
Textbox1 --> Angka
command button 1 --> btn1
command button 2 --> btn2
command button 3 --> btn3
command button 4 --> btn4
command button 5 --> btn5
command button 6 --> btn6
command button 7 --> btn7
command button 8 --> btn8
command button 9 --> btn9
command button 0 --> btn0
command button + --> btnTambah
command button = --> btnHasil
command button - --> btnKurang
command button +/- --> btnMinus
command button X --> btnKali
command button / --> btnBagi
command button C --> btnC
command button Backspace --> btnback


Adapun listing program calculator pada visual basic dapat dilihat berikut ini :

Dim OPnya As String
Dim a As Double
Dim b As Double
Dim c As Double
Dim bil As String


Function Proseskan(op As String)
    Dim xhasil As Double
    Select Case op
    Case "+": xhasil = a + b
    Case "-": xhasil = a - b
    Case "/": xhasil = a / b
    Case "*": xhasil = a * b
    End Select
    Proseskan = xhasil
End Function

Private Sub btn1_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "1"
    Else
       bil = "1"
    End If
    Angka = bil
End Sub

Private Sub btn2_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "2"
    Else
       bil = "2"
    End If
    Angka = bil
End Sub

Private Sub btn3_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "3"
    Else
       bil = "3"
    End If
    Angka = bil
End Sub

Private Sub btn4_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "4"
    Else
       bil = "4"
    End If
    Angka = bil
End Sub

Private Sub btn5_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "5"
    Else
       bil = "5"
    End If
    Angka = bil
End Sub

Private Sub btn6_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "6"
    Else
       bil = "6"
    End If
    Angka = bil
End Sub

Private Sub btn7_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "7"
    Else
       bil = "7"
    End If
    Angka = bil
End Sub

Private Sub btn8_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "8"
    Else
       bil = "8"
    End If
    Angka = bil
End Sub
Private Sub btn9_Click()

    If bil <> "0" Or OPnya = "" Then
       bil = bil + "9"
    Else
       bil = "9"
    End If
    Angka = bil
End Sub
Private Sub btn0_Click()
    If bil <> "0" Or OPnya = "" Then
       bil = bil + "0"
    Else
       bil = "0"
    End If
    Angka = bil
End Sub

Private Sub btnback_Click()
    Dim jml As Byte
    jml = Len(bil)
    If jml > 0 Then
        bil = Left(bil, jml - 1)
        Angka = bil
    End If
End Sub

Private Sub btnbagi_Click()
    If bil <> "" Then
        OPnya = "/"
        If a = 0 Then
            a = CCur(bil)
        Else
            b = CCur(bil)
            a = Proseskan("/")
        End If
        bil = ""
    End If
End Sub

Private Sub btnC_Click()
    a = 0
    b = 0
    c = 0
    bil = ""
    OPnya = ""
    Angka = "0"
End Sub

Private Sub btnhasil_Click()
    b = CCur(bil)
    Angka = Proseskan(OPnya)
    bil = "0"
    a = 0
    b = 0
End Sub

Private Sub btnkali_Click()
    If bil <> "" Then
        OPnya = "*"
        If a = 0 Then
            a = CCur(bil)
        Else
            b = CCur(bil)
            a = Proseskan("*")
        End If
        bil = ""
    End If
End Sub

Private Sub btnkoma_Click()
    If bil <> "" Then
        bil = bil + ","
    Else
        bil = "0"
    End If
    Angka = bil
End Sub

Private Sub btnkurang_Click()
    If bil <> "" Then
        OPnya = "-"
        If a = 0 Then
            a = CCur(bil)
        Else
            b = CCur(bil)
            a = Proseskan("-")
        End If
        bil = ""
    End If
End Sub

Private Sub btnminus_Click()
    bil = CCur(bil) * -1
    Angka = bil
End Sub

Private Sub btntambah_Click()
    If bil <> "" Then
        OPnya = "+"
        If a = 0 Then
            a = CCur(bil)
        Else
            b = CCur(bil)
            a = Proseskan("+")
        End If
        bil = ""
    End If
End Sub

Private Sub Form_Load()
    operatornya = ""
    bil = ""
    a = 0
    b = 0
    c = 0
    Angka = "0"
End Sub

ok ! bagaimana mudah bukan?
Share this article :

Post a Comment

 
Support : Hafiz_Blogger | haffiz Template | Mas Hafiz
Copyright © 2011. AhmaD_Haffiz - All Rights Reserved
Template Created by Creating hafiz_website Published by Mas Hafiz
Proudly powered by Blogger