CONTOH KODE DALAM BAB 3
1. Membangkitkan kejadian Click pada CommandButton . Komponen
Kontrol P roperti
CommandButton Name = Command1 Caption = Tombol 1 CommandButton Name = Command2 Caption = Tombol 2
Label Name = Label1
Kondisi a w al saat pendesainan
Kode :
Private Sub Command1_Click()
Command2.Value = True ‘ Sama artinya dgn memanggil Command2_Click() End Sub
Private Sub Command2_Click()
Label1.Caption = "Aku hasil penekanan Tombol 2"
End Sub
Kondisi aplikasi saat run-time.
Ketika anda mengklik tombol Command1, maka secara otomatis anda
juga menekan tombol Command2. Sehingga penekanan tombol kedua
menghasilkan penulisan teks di Label1.
Komponen
Kontrol P roperti
Frame Name = fraMonitor
Caption = Jenis Monitor OptionButton Name = optSamsung
Caption = Samsung OptionButton Name = optToshiba Caption = Toshiba OptionButton Name = optLG
Caption = LG OptionButton Name = optGTC
Caption = GTC
Frame Name = fraProsesor
Caption = Jenis Prosesor OptionButton Name = optAMD
Caption = AMD OptionButton Name = optPentium
Caption = Pentium
Frame Name = fralain
Caption = Komponen Lain
CheckBox Name = chkKeyboard
Caption = Keyboard
CheckBox Name = chkMouse
Caption = Mouse
CheckBox Name = chkUPS
Caption = UPS CheckBox Name = chkStabilizer
Caption = Stabilizer
Label Name = lblMonitor
Caption = Monitor Pilihan
Label Name = lblProsesor
Caption = Prosesor Pilihan
Label Name = lblLain
Caption = Komponen Lain
TextBox Name = txtMonitor
Text = {kosongkan isinya}
TextBox Name = txtProsesor
Text = {kosongkan isinya}
TextBox
Name = txtLain Multiline = True ScrollBars = 2-Vertical Text = {kosongkan isinya}
CommandButton Name = cmdReset
Caption = Reset
Kode :
Private Sub chkKeyboard_Click() If chkKeyboard.Value = vbChecked Then txtLain = txtLain + "Keyboard" + vbNewLine End If
End Sub
Private Sub chkMouse_Click() If chkMouse.Value = vbChecked Then txtLain = txtLain + "Mouse" + vbNewLine End If
End Sub
Private Sub chkStabilizer_Click()
If chkStabilizer.Value = vbChecked Then txtLain = txtLain + "Stabilizer" + vbNewLine End If
End Sub
Private Sub chkUPS_Click() If chkUPS.Value = vbChecked Then txtLain = txtLain + "UPS" + vbNewLine End If
End Sub
Private Sub cmdReset_Click()
txtMonitor = "" ‘ Mengosongkan isi semua textbox secara manual txtProsesor = ""
txtLain = ""
End Sub
Private Sub optAMD_Click() txtProsesor = "AMD"
End Sub
Private Sub optGTC_Click() txtMonitor = "GTC"
End Sub
Private Sub optLG_Click() txtMonitor = "LG"
End Sub
Private Sub optPentium_Click() txtProsesor = "Pentium"
End Sub
Private Sub optSamsung_Click() txtMonitor = "Samsung"
End Sub
Private Sub optToshiba_Click() txtMonitor = "Toshiba"
End Sub
Setiap kali anda mengklik suatu OptionButton, maka akan mengisi textbox yang telah ditentukan dengan isi teks yang telah ditentukan pula. Ketika tombol Reset ditekan, maka seluruh textbox isinya dikosongkan.
3. Contoh penggunaan kontrol ComboBox dan ListBox Komponen
Kontrol P roperti
Label Name = lblKelamin
Caption = Jenis Kelamin
Label Name = lblKartu
Caption = Kartu Pilihan
Label Name = lblHasil
Caption = Hasil
ComboBox Name = cboKelamin
Style = 2-Dropdown List
ListBox Name = lstKartu
MultiSelect = 0-None CommandButton Name = cmdProses
Caption Proses
TextBox Name = txtHasil
Kondisi aplikasi saat run-time
Kode :
Option Explicit
Private Sub cmdProses_Click()
‘ Memanggil isi dalam combobox dan listbox txtHasil = cboKelamin.Text + " - " + lstkartu.Text End Sub
Private Sub Form_Load() cboKelamin.Clear
cboKelamin.AddItem "Pria"
cboKelamin.AddItem "Wanita"
cboKelamin.ListIndex = 0 ' Menampilkan data yang pertama
lstkartu.Clear
lstkartu.AddItem "Golf Member"
lstkartu.AddItem "Kartu Alumni"
lstkartu.AddItem "Kartu ATM"
lstkartu.AddItem "KTP"
End Sub
4. Contoh penggunaan Kontrol Vertical ScrollBar Komponen
Kontrol P roperti
Label Name = lblRed
Caption = RED
Label Name = lblGreen
Caption = GREEN
Label Name = lblBlue
Caption = BLUE
Label Name = lblAngkaRed
Label Name = lblAngkaGreen
Label Name = lblAngkaBlue
Label Name = lblWarna
VScrollBar
Name = VScroll1 Max = 255 Min = 0 VScrollBar
Name = VScroll2 Max = 255 Min = 0 VScrollBar
Name = VScroll3
Max = 255
Min = 0
Option Explicit
Private Sub VScroll1_Change()
lblAngkaRed.Caption = VScroll1.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Private Sub VScroll1_Scroll()
lblAngkaRed.Caption = VScroll1.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Private Sub VScroll2_Change()
lblAngkaGreen.Caption = VScroll2.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Private Sub VScroll2_Scroll()
lblAngkaGreen.Caption = VScroll2.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Private Sub VScroll3_Change()
lblAngkaBlue.Caption = VScroll3.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Private Sub VScroll3_Scroll()
lblAngkaBlue.Caption = VScroll3.Value
lblWarna.BackColor = RGB(VScroll1.Value, VScroll2.Value, VScroll3.Value) End Sub
Kondisi aplikasi saat run-time
5. Penggunaan kontrol Drive, Dir, dan File ListBox Komponen
Kontrol P roperti
DriveListBox Name = Drive1
DirListBox Name = Dir1
FileListBox Name = File1
Label Name = lblJalur
TextBox Name = txtJalur
Kode :
Option Explicit
Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub
Private Sub Drive1_Change()
' Properti Drive juga mengembalikan label volume.
Dir1.Path = Left$(Drive1.Drive, 1) & ":\"
End Sub
Private Sub File1_Click()
txtJalur = Dir1.Path + "\" + File1.FileName End Sub