Contoh Aplikasi Kalkulator
1. Buatlah user interface (UI) seperti berikut2. Atur property dan value masing-masing object seperti berikut,
Object | Property | Value |
---|---|---|
Form1 | Caption | Kalkulator Sederhana |
Text1 | - | - |
Command1 | name Caption Index |
cmdAngka 1 0 |
Command1 | name Caption Index |
cmdAngka 2 1 |
Command1 | name Caption Index |
cmdAngka 3 2 |
Command1 | name Caption Index |
cmdAngka 4 3 |
Command1 | name Caption Index |
cmdAngka 5 4 |
Command1 | name Caption Index |
cmdAngka 6 5 |
Command1 | name Caption Index |
cmdAngka 7 6 |
Command1 | name Caption Index |
cmdAngka 8 7 |
Command1 | name Caption Index |
cmdAngka 9 8 |
Command1 | name Caption Index |
cmdAngka 0 9 |
Command1 | name Caption Index |
cmdAngka , 10 |
Command2 | name Caption Index |
cmdOperator + 0 |
Command2 | name Caption Index |
cmdOperator - 1 |
Command2 | name Caption Index |
cmdOperator * 2 |
Command2 | name Caption Index |
cmdOperator / 3 |
Command2 | name Caption Index |
cmdOperator 1/X 4 |
Command3 | name Caption |
cmdHapus C |
Command4 | name Caption |
cmdHitung = |
3. Buka jendela kode (code editor) seperti gambar berikut,
4. Copy dan paste kode program berikut kedalam jendela kode (code editor) visual basic 6.0,
Dim angka(1 To 2) As Single
Dim operator As String
Private Sub cmdAngka_Click(Index As Integer)
Text1.Text = Text1.Text & cmdAngka(Index).Caption
End Sub
Private Sub cmdOperator_Click(Index As Integer)
If Text1.Text = "" Then Exit Sub
angka(1) = CSng(Text1.Text)
operator = cmdOperator(Index).Caption
Text1.Text = ""
End Sub
Private Sub cmdHitung_Click()
Dim hasil As Single
If Text1.Text = "" Then Exit Sub
angka(2) = CSng(Text1.Text)
Select Case operator
Case "+"
hasil = angka(1) + angka(2)
Case "-"
hasil = angka(1) - angka(2)
Case "*"
hasil = angka(1) * angka(2)
Case "/"
hasil = angka(1) / angka(2)
End Select
Text1.Text = hasil
End Sub
Private Sub cmdHapus_Click()
Text1.Text = ""
End Sub
Makasih sekali ilmunya mbak
BalasHapus