Entis Sutisna

Rabu, 09 Maret 2016

cara membuat kalkulator sederhana


Apakah anda ingin membuat kalkulator sederhana? Jangan khawatir disini saya akan berikan koding membuat kalkulator sederhana menggunakan vb 2008 yang dapat digunakan sebagai referensi anda. Langsung saja ikuti langkah-langkahnya!
1. Buka aplikasi vb 2008 lalu klik create project

VB
2. Pilih Visual Basic dan namai project dengan nama kalkulator lalu klik ok.

vb2
3. Pada lembar kerja form3 buatlah tombol-tombol seperti gambar di bawah ini dengan cara melakukan double klik/klik pada tombol-tombol yang berada di All Windows Form.


vb3
vb4 

4. Aturlah masing-masing attribute properties tombol sebagai berikut :
tbl1

5. Maka tampilannya akan berubah menjadi seperti  gambar berikut :
vb5

6. Mengisi kode pada masing-masing tombol dengan kode dibawah ini :
Public Class Form3
Dim operan1 As String = “”
Dim operan2 As String = “”
Dim hasil As Decimal = 0
Dim tambah As Boolean = False
Dim kurang As Boolean = False
Dim kali As Boolean = False
Dim bagi As Boolean = False
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cmd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd1.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “1”
txtdisplay.Text = operan1
Else
operan2 += “1”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “2”
txtdisplay.Text = operan1
Else
operan2 += “2”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd3.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “3”
txtdisplay.Text = operan1
Else
operan2 += “3”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmdtambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdtambah.Click
txtdisplay.Text = “+”
tambah = True
End Sub
Private Sub cmd4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “4”
txtdisplay.Text = operan1
Else
operan2 += “4”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd5.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “5”
txtdisplay.Text = operan1
Else
operan2 += “5”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd6.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “6”
txtdisplay.Text = operan1
Else
operan2 += “6”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmdkurang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdkurang.Click
txtdisplay.Text = “-”
kurang = True
End Sub
Private Sub cmd7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd7.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “7”
txtdisplay.Text = operan1
Else
operan2 += “7”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd8.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “8”
txtdisplay.Text = operan1
Else
operan2 += “8”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmd9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd9.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “9”
txtdisplay.Text = operan1
Else
operan2 += “9”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmdkali_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdkali.Click
txtdisplay.Text = “x”
kali = True
End Sub
Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
operan1 = “”
operan2 = “”
txtdisplay.Text = “”
kali = False
bagi = False
kurang = False
tambah = False
End Sub
Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click
If (tambah = False And kurang = False And kali = False And bagi = False) Then
operan1 += “0”
txtdisplay.Text = operan1
Else
operan2 += “0”
txtdisplay.Text = operan2
End If
End Sub
Private Sub cmdsamadengan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsamadengan.Click
If tambah = True Then
hasil = Val(operan1) + Val(operan2)
txtdisplay.Text = hasil
operan1 = hasil
operan2 = “”
tambah = False
ElseIf kurang = True Then
hasil = Val(operan1) – Val(operan2)
txtdisplay.Text = hasil
operan1 = hasil
operan2 = “”
kurang = False
ElseIf kali = True Then
hasil = Val(operan1) * Val(operan2)
txtdisplay.Text = hasil
operan1 = hasil
operan2 = “”
kali = False
ElseIf bagi = True Then
If operan2 = “0” Then
txtdisplay.Text = “Error”
operan2 = “”
Else
hasil = Val(operan1) / Val(operan2)
txtdisplay.Text = hasil
operan1 = hasil
operan2 = “”
bagi = False
End If
End If
End Sub
Private Sub cmdbagi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdbagi.Click
txtdisplay.Text = “/”
bagi = True
End Sub
End Class

7. Lalu tekan F5 untuk mengkompile/menjalankan program.


Selamat mencoba!

0 komentar:

Posting Komentar