Posted by : Erick Elfrada Sinurat Minggu, 24 Juni 2012

 Kode Source Kalkulator Pada VB Cukup sederhana yaitu: ...


Option Explicit
Private Hasil As Double
Private Const opNol = 0
Private Const opTambah = 1
Private Const opKurang = 2
Private Const opkali = 3
Private Const opBagi = 4

Private Operator As Integer
Private NilaiBaru As Boolean

//untuk menghapus karakter terakhir
Private Sub Hapus()
Dim txt As String
Dim min_len As Interger

txt = txtdisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If

If Len(txt) > min_len Then
txtdisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtdisplay.Text = "0"
End Sub

Private Sub cmdaAngka_Click(Index As Integer)
End Sub

//hapus angka, hasil terakhir dan operator
Private Sub cmdClear_Click()
cmdClearEntry_Click
Hasil = 0
Operator = opNol
End Sub

'hapus angka
Private Sub cmdClearEntry_Click()
txtdisplay.Text = ""
End Sub

//menambahkan koma (desimal)
Private Sub cmdKoma_Click()
If InStr(txtdisplay.Text, ".") Then
Beep
Else
If NilaiBaru Then
txtdisplay.Text = "."
NilaiBaru = False
Else
txtdisplay.Text = txtdisplay.Text & "."
End If
End If
End Sub

//Menghitung
Private Sub cmdSamaDengan_click()
Dim HasilBaru As Double

If txtdisplay.Text = "" Then
HasilBaru = 0
Else
HasilBaru = CDbl(txtdisplay.Text)
End If
Select Case Operator
Case opNol
Hasil = HasilBaru
Case opTambah
Hasil = Hasil + HasilBaru
Case opKurang
Hasil = Hasil - HasilBaru
Case opkali
Hasil = Hasil * HasilBaru
Case opBagi
//Tidak bisa dibagi Nol
If HasilBaru = 0 Then
MsgBox "Tidak bisa dibagi 0", vbOKOnly + vbCritical, "Kalku ERROR"
Call cmdClear_Click
Else
Hasil = Hasil / HasilBaru
End If
End Select
Operator = opNol
NilaiBaru = True
txtdisplay.Text = Format$(Hasil)
End Sub

//menuliskan angka
Private Sub cmdAngka_Click(Index As Integer)
If NilaiBaru Then
txtdisplay.Text = Format$(Index)
NilaiBaru = False
Else
txtdisplay.Text = "_"
txtdisplay.Text = Format$(Index)
End If
End Sub

//tombol tambah/kurang/bagi/kali
Private Sub cmdOperator_Click(Index As Integer)

cmdSamaDengan_click
Operator = Index
NilaiBaru = True
End Sub

//merubah tanda +/-
Private Sub cmdPlusMinus_Click()
If NilaiBaru Then
txtdisplay.Text = "-"
ElseIf Left$(txtdisplay.Text, 1) = "-" Then
txtdisplay.Text = Right$(txtdisplay.Text, 2)
Else
txtdisplay.Text = "-" & txtdisplay.Text
End If
End Sub

//filter untuk angka saja yang bisa diketikan
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub

//supaya kursor tetap di kanan
Private Sub txtDisplay_Change()
txtdisplay.SelStart = Len(txtdisplay.Text)
End Sub

Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub

//untuk mengetikkan angka di keyboard
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String

ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdAngka_Click 0
Case "1"
cmdAngka_Click 1
Case "2"
cmdAngka_Click 2
Case "3"
cmdAngka_Click 3
Case "4"
cmdAngka_Click 4
Case "5"
cmdAngka_Click 5
Case "6"
cmdAngka_Click 6
Case "7"
cmdAngka_Click 7
Case "8"
cmdAngka_Click 8
Case "9"
cmdAngka_Click 9
Case "*", "x", "X"
cmdOperator_Click opkali
Case "+"
cmdOperator_Click opTambah
Case vbCrLf, vbCr, "="
cmdSamaDengan_click
Case "-"
cmdOperator_Click opKurang
Case "."
cmdKoma_Click
Case "/"
cmdOperator_Click opBagi
Case "C", "c"
cmdClearEntry_Click
End Select

KeyAscii = 0
End Sub

//untuk ketikan angka di numpad
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdAngka_Click 0
Case vbKeyNumpad1
cmdAngka_Click 1
Case vbKeyNumpad2
cmdAngka_Click 2
Case vbKeyNumpad3
cmdAngka_Click 3
Case vbKeyNumpad4
cmdAngka_Click 4
Case vbKeyNumpad5
cmdAngka_Click 5
Case vbKeyNumpad6
cmdAngka_Click 6
Case vbKeyNumpad7
cmdAngka_Click 7
Case vbKeyNumpad8
cmdAngka_Click 8
Case vbKeyNumpad9
cmdAngka_Click 9
Case vbKeyMultiply
cmdOperator_Click opkali
Case vbKeyAdd
cmdOperator_Click opTambah
Case vbKeySeparator
cmdSamaDengan_click
Case vbKeySubtract
cmdOperator_Click opKurang
Case vbKeyDivide
cmdOperator_Click opBagi
Case vbKeyDecimal
cmdKoma_Click
Case vbKeyBack, vbKeyDelete
Hapus

End Select
KeyCode = 0
End Sub

Leave a Reply

Subscribe to Posts | Subscribe to Comments

^^Find Me^^

^^Translate^^

^^God Enel^^

^^God Enel^^
My Favorite Anime Character

- Copyright © Elfrada.com - You'll Get it Here-|-Robotic Notes-|-Powered by Blogger -|- Designed by Erick Elfrada -