Membuat Kalkulator di Visual Basic – Bagi seorang pemula seperti saya, dapat membuat suatu program kalkulator adalah suatu keharusan, demakin banyak fungsi atau tombol-tombol yang tersedia maka semakin bagus.
Satu program kalkulator yang saya contohkan ini merupakan program kalkulator versi pertama saya, tanpa revisi apapun. Karena yang versi revisinya dah hilang (bareng sama flashdisk). Jadi jujur saja, yang satu ini memang agak kurang sempurna.
Untuk sedikit keterangan bahwa fungsi untuk sinus radius dan tangent yang digunakan adalah mode radius serta sesungguhnya ada 3 textbox di program kalkulator ini, namun textbox yang ke-3 tipe visible-nya dibuat false. Sedangkan untuk design form, tampak seperti gambar diatas.
Untuk lebih lengkapnya anda bisa melihat contoh kode-kode yang saya buat.
Dim nilai1, nilai2, hasil As Double
Dim tanda, unik As Integer
Private Sub Command1_Click() ‘Tombol angka 1.
Text1.Text = Text1.Text & “1″
If tanda > 0 Then
Text3.Text = Text3.Text & “1″
End If
End Sub
Private Sub Command10_Click() ‘Tombol angka 0.
Text1.Text = Text1.Text & “0″
If tanda > 0 Then
Text3.Text = Text3.Text & “0″
End If
End Sub
Private Sub Command11_Click() ‘Tombol bentuk positif / negatif
If tanda > 0 Then
Text1.Text = Text1.Text & “-”
unik = 1 ‘VARIABLE unik 1 UNTUK bentuk negatif
Exit Sub
End If
Text1.Text = Text1.Text * -1
End Sub
Private Sub Command12_Click() ‘Tombol bentuk DESIMAL
Text1.Text = Text1.Text & “.”
unik = 2 ‘VARIABLE unik 2 UNTUK DESIMAL
End Sub
Private Sub Command13_Click() ‘Tombol TAMBAH
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 1
Text1.Text = Text1.Text & “+”
Exit Sub
End If
nilai1 = Text1.Text
tanda = 1
Text1.Text = Text1.Text & “+”
End Sub
Private Sub Command14_Click() ‘Tombol KURANG
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 2
Text1.Text = Text1.Text & “-”
Exit Sub
End If
nilai1 = Text1.Text
tanda = 2
Text1.Text = Text1.Text & “-”
End Sub
Private Sub Command15_Click() ‘TOMBOL KALI
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 3
Text1.Text = Text1.Text & “x”
Exit Sub
End If
nilai1 = Text1.Text
tanda = 3
Text1.Text = Text1.Text & “x”
End Sub
Private Sub Command16_Click() ‘TOMBOL BAGI
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 4
text1.Text = Text1.Text & “/”
Exit Sub
End If
nilai1 = Text1.Text
tanda = 4
Text1.Text = Text1.Text & “/”
End Sub
Private Sub Command17_Click() ‘TOMBOL SAMA DENGAN
nilai2 = Text3.Text
If unik = 1 Then
nilai2 = nilai2 * -1
End If
Select Case tanda
Case 1
hasil = (nilai1 – 1) + (nilai2 + 1) ‘PENJUMLAHAN
Case 2
hasil = nilai1 – nilai2 ‘PENGURANGAN
Case 3
hasil = nilai1 * nilai2 ‘PERKALIAN
Case 4
If nilai2 = 0 Then
MsgBox ” TERJADI ERROR, PEMBAGIAN DENGAN O “, vbCritical, “Error”
Text1.Text = “”
Exit Sub
End If
hasil = nilai1 / nilai2 ‘PEMBAGIAN
Case 5
hasil = nilai1 * nilai1 ‘KUADRAT
Case 6
hasil = nilai1 ^ nilai2 ‘PANGKAT n
Case 7
hasil = nilai2
Case 8
hasil = Sin(CDbl(nilai2))
Case 9
hasil = Cos(CDbl(nilai2))
Case 10
hasil = Tan(CDbl(nilai2))
Case 11
If nilai2 < 0 Then MsgBox "TIDAK DAPAT MENGHITUNG NILAI AKAR DARI BILANGAN NEGATIF ", vbInformation, "PEMBERITAHUAN ERROR" Exit Sub End If hasil = Sqr(nilai2) End Select If unik = 2 Then hasil = hasil * 0.1 End If tanda = 50 Text3.Text = "" Text2.Text = hasil End Sub Private Sub Command18_Click() ‘TOMBOL CLEAR tanda = 0 Text1.Text = "" Text2.Text = "" Text3.Text = "" End Sub Private Sub Command19_Click() ‘TOMBOL QUIT MsgBox "Coded by : T. Erick Sitorus " & vbCrLf & "Known As : WONDERKID ", vbInformation, “KALKULATOR INI" End End Sub Private Sub Command2_Click() ‘TOMBOL ANGKA 2 Text1.Text = Text1.Text & "2" If tanda > 0 Then
Text3.Text = Text3.Text & “2″
End If
End Sub
Private Sub Command20_Click() ‘TOMBOL CLEAR ALL
tanda = 0
Text1.Text = “”
Text2.Text = “”
Text3.Text = “”
End Sub
Private Sub Command21_Click() ‘TOMBOL KUADRAT
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 5
Text1.Text = Text1.Text & “^2″
Exit Sub
End If
nilai1 = Text1.Text
tanda = 5
Text1.Text = Text1.Text & “^2″
End Sub
Private Sub Command22_Click() ‘TOMBOL PANGKAT n
If tanda = 50 Then
Text1.Text = hasil
nilai1 = Text2.Text
tanda = 6
Text1.Text = Text1.Text & “^”
Exit Sub
End If
nilai1 = Text1.Text
tanda = 6
text1.Text = Text1.Text & “^”
End Sub
Private Sub Command23_Click() ‘TOMBOL ABOUT
MsgBox “THIS PROGRAM WAS CREATED BY T. ERICK SITORUS, THIS PROGRAM IS MADE ONLY TO CALCULATE SIMPLE MATHEMATIC OPERATION”, vbCritical, “About This Program”
End Sub
Private Sub Command24_Click() ‘TOMBOL PERSEN
Text3.Text = Text3.Text * 0.01
Text1.Text = Text1.Text & “%”
End Sub
Private Sub Command25_Click() ‘TOMBOL FAKTORIAL
Text1.Text = hasil
nilai1 = Text1.Text
tanda = 7
Text1.Text = Text1.Text & “!”
For a = nilai1 – 1 To 1 Step -1
nilai1 = nilai1 * a
Next a
Text3.Text = nilai1
End Sub
Private Sub Command26_Click() ‘TOMBOL SINUS
Text1.Text = “”
tanda = 8
Text1.Text = Text1.Text & “sin”
End Sub
Private Sub Command27_Click() ‘TOMBOL COSINUS
Text1.Text = “”
tanda = 9
Text1.Text = Text1.Text & “cos”
End Sub
Private Sub Command28_Click() ‘TOMBOL TANGEN
Text1.Text = “”
tanda = 10
Text1.Text = Text1.Text & “tan”
End Sub
Private Sub Command29_Click() ‘TOMBOL AKAR
tanda = 11
Text1.Text = Text1.Text & “sqr”
End Sub
Private Sub Command3_Click() ‘TOMBOL ANGKA 3
Text1.Text = Text1.Text & “3″
If tanda > 0 Then
Text3.Text = Text3.Text & “3″
End If
End Sub
Private Sub Command30_Click() ‘TOMBOL BACKSPACE
If Text1.Text = “” Then
MsgBox “LAYAR SUDAH KOSONG “, vbInformation, “INFO”
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) – 1)
If tanda > 0 Then
tanda = 0
End If
End Sub
Private Sub Command4_Click() ‘TOMBOL ANGKA 4
Text1.Text = Text1.Text & “4″
If tanda > 0 Then
Text3.Text = Text3.Text & “4″
End If
End Sub
Private Sub Command5_Click() ‘TOMBOL ANGKA 5
Text1.Text = Text1.Text & “5″
If tanda > 0 Then
Text3.Text = Text3.Text & “5″
End If
End Sub
Private Sub Command6_Click() ‘TOMBOL ANGKA 6
Text1.Text = Text1.Text & “6″
If tanda > 0 Then
Text3.Text = Text3.Text & “6″
End If
End Sub
Private Sub Command7_Click() ‘TOMBOL ANGKA 7
Text1.Text = Text1.Text & “7″
If tanda > 0 Then
Text3.Text = Text3.Text & “7″
End If
End Sub
Private Sub Command8_Click() ‘TOMBOL ANGKA 8
Text1.Text = Text1.Text & “8″
If tanda > 0 Then
Text3.Text = Text3.Text & “8″
End If
End Sub
Private Sub Command9_Click() ‘TOMBOL ANGKA 9
Text1.Text = Text1.Text & “9″
If tanda > 0 Then
Text3.Text = Text3.Text & “9″
End If
End Sub
Private Sub Form_Load() ‘SAAT FORM LOAD
tanda = 0
End Sub
Read more: http://www.hasbihtc.com/membuat-kalkulator-di-visual-basic.html#ixzz2odgAqJa2
Home » Pemograman » Membuat kalkulator di visuak basic Konten Lain di Sini
0 comments
Tambahkan Komentar Anda
semua berawal dari diri kita