Cara Membuat Aplikasi Kalkulator Dengan Menggunakan Visual Basic 6.0
Membuat Aplikasi Kalkulator Dengan Menggunakan Visual Basic 6.0
Masih dengan visual basic, di posting kali ini saya membahas cara membuat program aplikasi kalkulator dengan fungsi +, -, *, / dan ln saja karena postingan ini merupakan sharean dari PR yang dibebankan dosen kepada saya. Mari langsung kita bahas cara membuat aplikasi kalkulator sederhana ini.1) Buat desain kalkulator seperti gambar dibawah ini atau dengan desain anda sendiri,
Gambar 1. Desain Kalkulator
Gambar 2. Keterangan Desain
2) Kemudian Copy paste listing program dibawah ini ;
Dim a As String
Dim i, X As Integer
Dim no1 As Double, cnt1 As Integer
Dim op As String, op1 As String
Private Sub cmdnumb_Click(Index As Integer)
If txt1.Text = "0" Then
txt1.Text = ""
End If
If X = 0 Then
txt1.Text = ""
X = X + 1
End If
txt1.Text = txt1.Text + cmdnumb(Index).Caption
txt2.Text = txt2.Text + cmdnumb(Index).Caption
End Sub
Private Sub Form_Activate()
txt1.Visible = False
txt2.Visible = False
End Sub
Private Sub cmdon_Click(Index As Integer)
txt1.Visible = True
txt2.Visible = True
End Sub
Private Sub cmdoff_Click(Index As Integer)
txt1.Visible = False
txt2.Visible = False
End Sub
Private Sub cmdopr_Click(Index As Integer)
X = 0
If cnt1 > 0 Then
Select Case op
Case "+"
ans = no1 + txt1.Text
txt1.Text = ans
no1 = txt1.Text
op = Cmdopr(Index).Caption
Case "-"
ans = no1 - txt1.Text
txt1.Text = ans
no1 = txt1.Text
op = Cmdopr(Index).Caption
Case "/"
ans = no1 / txt1.Text
txt1.Text = ans
no1 = txt1.Text
op = Cmdopr(Index).Caption
Case "*"
ans = no1 * txt1.Text
txt1.Text = ans
no1 = txt1.Text
op = Cmdopr(Index).Caption
End Select
Else
no1 = Val(txt1.Text)
op = Cmdopr(Index).Caption
cnt1 = cnt1 + 1
End If
txt2.Text = txt2.Text + Cmdopr(Index).Caption
End Sub
Private Sub cmdc_Click()
Select Case Index
Case 0
cnt1 = 0
X = 0
txt1.Text = ""
txt2.Text = ""
txt1.Visible = True
Case 1
txt2.Text = Mid(txt2.Text, 1, Len(txt2.Text) - 1)
End Select
End Sub
Private Sub cmdent_Click()
cnt1 = 0
Select Case op
Case "+"
ans = txt1.Text + no1
If cnt = 0 Then
no1 = txt1.Text
cnt = cnt + 1
End If
txt1.Text = ans
Case "-"
If cnt > 0 Then
txt1.Text = no1
no1 = ans
End If
ans = no1 - txt1.Text
no1 = txt1.Text
txt1.Text = ans
cnt = cnt + 1
Case "*"
ans = no1 * txt1.Text
If cnt = 0 Then
no1 = txt1.Text
cnt = cnt + 1
End If
txt1.Text = ans
Case "/"
If cnt > 0 Then
txt1.Text = no1
no1 = ans
End If
If txt1.Text = "0" Then
i = MsgBox("Divide by zero error", vbExclamation + vbOKOnly, "ERROR")
Else
ans = no1 / txt1.Text
no1 = txt1.Text
txt1.Text = ans
cnt = cnt + 1
End If
End Select
txt1.Text = txt1.Text
txt1.Visible = True
End Sub
Private Sub cmdln_Click(Index As Integer)
op = Cmdln(Index).Caption
nos = Val(txt1.Text)
Select Case op
Case "ln"
If nos = 0 Then
i = MsgBox("Math error", vbExclamation + vbOKOnly, "ERROR")
Else
ans = Log(nos)
End If
End Select
txt2.Text = ans
End Sub
3) Setetalah selesai anda desain dan listing programnya sudah di copy paste silahkan anda running dengan mengklik tombol running atau dengan menekan tombol key F5 sehingga hasilnya tampak seperti gambar di bawah ini ;

Gambar 2. Hasil Running Kalkulator
4) Untuk mengoperasikan aplikasi kalkulator tersebut klik terlebih dahulu tombol On, kemudian gunakanlah kalkulator tersebut sesuai dengan kebutuhan anda.
Begitulah cara membuat aplikasi kalkulator, semoga bermanfaat.
Comments