Penggunaan Array dan Manipulasi String Array
ELEMEN-ELEMEN DALAM VISUAL BASIC 6
Menu
Membuat menu sederhana dengan menggunakan Aplication Wizard Buat project baru dengan memilih New Project dari menu File.
Pilih VB Application Wizard dengan klik dua kali pada iconnya. Setelah tampil dialog Application Wizard tahap pertama, klik Next.
Kemudian pilih Single Document Interface (SDI), dan tuliskan nama project sesuai dengan keinginan anda !
+ Created: 28/08/2005
Pilih Menu dan Sub Menu yang anda inginkan kemudian klik Finish jika sudah selesai atau klik Next jika akan mengatur tampilan toolbar.
Pilihan Next selanjutnya akan memberikan kebebasan anda untuk memilih icon-icon toolbar yang akan di tampilkan.
Pilihan Next Selanjutnya adalah penentuan bahasa, dan bentuk-bentuk tampilan form yang diinginkan yang tidak berhubungan dengan tampilan menu kita, untuk saat ini klik Finish, kemudian klik OK jika tampilan dialog pembuatan aplikasi selesai dilakukan.
+ Created: 28/08/2005
Membuat menu dengan Menu Editor
1. Buat project baru dengan memilih Standard EXE.
2. Kemudian pilih Menu Editor pada menu Tools atau tekan tombol kombinasi CTRL+E. 3. Kemudian ketik &Edit pada Caption text box dan mnuEdit pada Name text box
4. Klik Next untuk membuat Menu berikutnya, atau tekan panah ke kanan untuk membuat Sub Menu.
5. Nilai Property yang umumnya digunakan dalam pembuatan menu adalah:
Properti Nilai/Tipe Penjelasan
Caption String Text yang akan di tampilkan
Checked Boolean Menampilkan tanda Check (√) pada sub menu.
Enabled Boolean Jika bernilai False maka Caption Menu akan berwarna abu-abu dan tidak bias diklik.
Name String Nama Object Menu.
Shortcut N/A Kombinasi tombol untuk sub menu atau pop up menu. 6. Buat contoh menu dengan ketentuan seperti di bawah ini:
+ Created: 28/08/2005
Caption Name Type ShortCut
&File mnuFile Menu Utama
&New mnuFileNew Sub Menu Ctrl-N
&Open mnuFileOpen Sub Menu Ctrl-O
&Save mnuFileSave Sub Menu Ctrl-S
Save &As … mnuFileSaveAs Sub Menu
- sep1 Separator
&Print mnuPrint Sub Menu Ctrl-P
E&xit mnuExit Sub Menu
&Edit mnuEdit Menu Utama
&Copy mnuCopy Sub Menu Ctrl-C
C&ut mnuCut Sub Menu Ctrl-X
&Paste mnuPaste Sub Menu Ctrl-V
7. Klik dua kali pada menu Exit, dan tuliskan perintah seperti di bawah ini:
Private Sub mnuExit_Click() End
End Sub
8. Hasil tampilan menu yang telah dibuat seperti di bawah ini
9. Untuk membuat menu Pop Up untuk menu file, klik dua kali pada Form dan tuliskan perintah di bawah ini pada event Form1_MouseDown:
10. Jalankan dengan menekan F5 dan tekan tombol kanan Mouse untuk menampilkan Pop Up menu.
+ Created: 28/08/2005
Membuat Menu Kompleks
File Menu Edit Menu
New Undo Open Cut Save Copy Settings Paste About Select All Exit
Object menu dengan menggunakan Menu Editor
Name Caption Level Shortcut
mnuFile &File 0 None itmNew &New 1 None itmOpen &Open 1 None itmSave &Save 1 None sepOne - (a hyphen) 1 None itmSettings Se&ttings 1 None itmBlackOnWhite Black On White 2 None itmWhiteOnBlack White On Black 2 None itmAbout &About 1 None
sepTwo - 1 None
itmExit E&xit 1 None mnuEdit &Edit 0 None itmUndo &Undo 1 Ctrl+Z sepThree - 1 None itmCut Cu&t 1 Ctrl+X itmCopy &Copy 1 Ctrl+C itmPaste &Paste 1 Ctrl+V sepFour - 1 None
+ Created: 28/08/2005
Tuliskan perintah-perintah di bawah ini:
Private Sub itmBlackOnWhite_Click() txtMain.BackColor = vbWhite txtMain.ForeColor = vbBlack itmBlackOnWhite.Checked = True itmWhiteOnBlack.Checked = False End Sub
Private Sub itmWhiteOnBlack_Click() txtMain.BackColor = vbBlack txtMain.ForeColor = vbWhite itmBlackOnWhite.Checked = False itmWhiteOnBlack.Checked = True End Sub
Menggunakan Object Clipboard
Method Description
Clear Membersihkan data pada Clipboard GetData Mengembalikan graphic dari Clipboard GetFormat Mengembalikan tipe data dalam Clipboard GetText Mengembalikan text dari Clipboard SetData Mengirim graphics ke Clipboard SetText Mengirim Text ke Clipboard Private Sub itmCopy_Click()
Clipboard.SetText txtMain.SelText End Sub
Private Sub itmPaste_Click() Dim Temp$
Dim strLeft As String Dim strRight As String Dim strFull As String
Temp$ = Clipboard.GetText(vbCFText) txtMain.SelText = Temp$
End Sub
Private Sub itmSelectAll_Click() txtMain.SelStart = 0
txtMain.SelLength = Len(txtMain.Text) End Sub
+ Created: 28/08/2005
Bekerja dengan Keyboard dan Mouse
Sistem Operasi Windows merupakan system operasi yang membagi beberapa bagian dari device-independent hardware seperti monitor, printer, keyboard, mouse dari program aplikasi.
Contoh penggunaan Keypress Event
Private Sub txtKey_KeyPress(KeyAscii As Integer) Dim strKeyPressed As String
Dim strAscii As String Dim Msg As String
strKeyPressed = Chr(KeyAscii) strAscii = CStr(KeyAscii)
Msg = "Kode ASCII : " & strAscii
Msg = Msg & ", " & "Karakter: " & strKeyPressed MsgBox Msg
End Sub
Bekerja dengan KeyUp dan KeyDown Event Tabel perbedaan nilai pada parameter Shift
Combination Key(s) Held Down Value
Shift 1 Ctrl 2 Alt 4 Shift+Ctrl 3 Shift+Alt 5 Ctrl+Alt 6 Shift+Ctrl+Alt 7
+ Created: 28/08/2005
Table konstanta untuk parameter KeyCode
Constant Key Constant Key
vbKeyLButton Left mouse button vbKeyRight → vbKeyRButton Right mouse button vbKeyDown ↓
vbKeyCancel Cancel vbKeySelect Select
vbKeyMButton Middle mouse button vbKeyPrint Print Screen
vbKeyBack Backspace vbKeyExecute Execute
vbKeyTab Tab vbKeySnapshot Snapshot
vbKeyClear Clear vbKeyInsert Insert
vbKeyReturn Enter vbKeyDelete Delete
vbKeyShift Shift vbKeyHelp Help
vbKeyControl Ctrl vbKeyNumlock Num Lock
vbKeyMenu Menu vbKeyA through vbKeyZ Althrough Z, respectively
vbKeyPause Pause vbKey0 through vbKey9 0 (zero) through 9, respectively
vbKeyCapital Caps Lock vbKeyNumpad0 0 through 9 on number pad, respectively
vbKeyEscape Esc through vbKeyNumpad9
vbKeySpace Spacebar vbKeyMultiply Multiplication sign (*) on number pad
vbKeyPageUp Page Up vbKeyAdd Plus sign (+) on
number pad
vbKeyPageDown Page Down vbKeySeparator Enter on number pad
vbKeyEnd End vbKeySubtract Minus sign (-) on
number pad
vbKeyHome Home vbKeyDecimal Decimal point (.) on number pad
vbKeyLeft ← vbKeyDivide Division sign (/) on
number pad
vbKeyUp ↑ vbKeyF1 through F1 through F16,
respectively vbKeyF16
Private Sub txtKey_KeyDown(KeyCode As Integer, Shift As Integer) Dim strKey As String
Select Case KeyCode Case vbKeyF1 strKey = "F1" Case vbKeyF2 strKey = "F2" Case vbKeyF3 strKey = "F3" Case vbKeyF4 strKey = "F4"
+ Created: 28/08/2005 Case vbKeyF5 strKey = "F5" Case vbKeyF6 strKey = "F6" Case vbKeyF7 strKey = "F7" Case vbKeyF8 strKey = "F8" Case vbKeyF9 strKey = "F9" Case vbKeyF10 strKey = "F10" Case vbKeyF11 strKey = "F11" Case vbKeyF12 strKey = "F12" Case vbKeyF13 strKey = "F13" Case vbKeyF14 strKey = "F14" Case vbKeyF15 strKey = "F15" Case vbKeyF16 strKey = "F16" Case Else
strKey = "Some other key" End Select
Select Case Shift Case 0
Form1.Caption = "No key down" Case 1
Form1.Caption = "Shift down" Case 2
Form1.Caption = "Ctrl down" Case 3
Form1.Caption = "Shift dan Ctrl down" Case 4
Form1.Caption = "Alt down" Case 5
Form1.Caption = "Shift dan Alt down" Case 6
Form1.Caption = "Ctrl dan Alt down" Case 7
Form1.Caption = "Shift,Ctrl,Alt down" End Select
txtKey.Text = "Key Down, Key: " & strKey End Sub
+ Created: 28/08/2005
Menggunakan property KeyPreview pada Form = True
Private Sub Form_KeyPress (KeyAscii As Integer) txtForward.SetFocus
txtBackward.Text = Chr(KeyAscii) & txtBackward.Text txtAscii.Text = txtAscii.Text & CStr(KeyAscii) & "," End Sub
Bekerja dengan Mouse
Table nilai pada parameter Button Mouse
Mouse Button(s) Pressed Button Parameter Value
Left 1
Right 2
Left and right 3
Middle 4
Left and middle 5 Right and middle 6
All 7
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim intCntr As Integer For intCntr = 0 To 10000
Form1.Caption = CStr(intCntr) Next
End Sub
Private Sub lblClear_Click() Picture1.Cls
+ Created: 28/08/2005
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim strShift As String
lblStatMouse.Caption = "Mouse Down" Select Case Button
Case 0
lblMouseKey.Caption = "" Case 1
lblMouseKey.Caption = "Tombol Kiri" Case 2
lblMouseKey.Caption = "Tombol Kanan" Case 3
lblMouseKey.Caption = "Tombol Kiri+Kanan" Case 4
lblMouseKey.Caption = "Tombol Tengah" Case 5
lblMouseKey.Caption = "Tombol Kiri+Tengah" Case 6
lblMouseKey.Caption = "Tombol Kanan+Tengah" Case 7
lblMouseKey.Caption = "Semua" End Select
Select Case Shift Case 0 strShift = "" Case 1 strShift = "Shift" Case 2 strShift = "Ctrl" Case 3 strShift = "Shift + Ctrl" Case 4 strShift = "Alt" Case 5
strShift = "Shift + Alt" Case 6
strShift = "Ctrl + Alt" Case 7
strShift = "Shift + Ctrl + Alt" End Select
lblKeyboard.Caption = strShift End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim strX, strY As String
strX = CStr(X / Screen.TwipsPerPixelX) strY = CStr(Y / Screen.TwipsPerPixelY) Picture1.Line -(X, Y)
lblKursor.Caption = "X: " & strX & " , Y: " & strY lblStatMouse.Caption = "Mouse Move"
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
lblStatMouse.Caption = "Mouse Up" End Sub
+ Created: 28/08/2005
Waktu dan Formatnya
Contoh program untuk menampilkan waktu.
Private Sub Form_Load()
lblTime.Caption = CStr(Time) lblDate.Caption = CStr(Date) lblDateTime.Caption = CStr(Now) End Sub
Private Sub Timer1_Timer() lblTime.Caption = CStr(Time) End Sub
Menggunakan fungsi format:
MyString$ = Format(Expression[, Format_String[, _ FirstDayofWeek[, FirstWeekOfYear]]]) Tabel penggunaan fungsi Format untuk Date dan Time
Format String Example Result
"Long Date" Format(36000, "Long Date") Friday, July 24, 1998 "Medium Date" Format(36000, "Medium Date") 24-Jul-98
"Short Date" Format(36000, "Short Date") 7/24/98 "Long Time" Format(0.874, "Long Time") 8:58:34 p.m. "Medium Time" Format(0.874, "Medium Time") 08:58 p.m. "Short Time" Format(0.874, "Short Time") 20:58 Tabel penggunaan fungsi Format untuk Numerik
Format String Example Result
"General Number" Format(36000, "General Number") 36000 "Currency" Format(36000, "Currency") $36,000.00 "Fixed" Format(36000, "Fixed") 36000.00 "Standard" Format(36000, "Standard") 36,000.00 "Percent" Format(36000, "Percent") 3600000.00% "Scientific" Format(36000, "Scientific") 3.60E+04 "Yes/No" Format(36000, "Yes/No") Yes "True/False" Format(36000, "True/False") True "On/Off" Format(36000, "On/Off") On
Atau dapat pula menggunaka Format(235.6, "###,##0.00") untuk menghasilkan 235.60,.
Menghitung perbedaan tanggal dengan menggunakan Datediff:
MyLong = DateDiff(Interval, Start_Date, End_Date[,FirstDayOfWeek[, FirstWeekOfYear]])
Tabel penggunaan fungsi Datediff
Value Interval Usage Return Value
"yyyy" Year DateDiff("yyyy", "7/4/76", "7/4/86") 10 "q" Quarter DateDiff("q", "7/4/76", "7/4/86") 40
+ Created: 28/08/2005
"m" Month DateDiff("m", "7/4/76", "7/4/86") 120 "y" Day of year DateDiff("y", "7/4/76", "7/4/86") 3652 "d" Day DateDiff("d", "7/4/76", "7/4/86") 3652 "w" Weekday DateDiff("w", "7/4/76", "7/4/86") 521 "ww" Week DateDiff("ww", "7/4/76", "7/4/86") 521 "h" Hour DateDiff("h", "7/4/76", "7/4/86") 87648 "n" Minute DateDiff("n", "7/4/76", "7/4/86") 5258880 "s" Second DateDiff("s", "7/4/76", "7/4/86") 315532800
Gunakan fungsi IsDate() untuk menyakinkan bahwa data tersebut merupakan tipe data Date.
Dim tglLahir As Date
Private Sub cmdStart_Click()
If Not IsDate(txtTglLahir.Text) Then Timer1.Enabled = False
lblDetik.Caption = "" lblHari.Caption = "" lblTahun.Caption = ""
MsgBox "Tuliskan tanggal lahirmu dengan benar (dd/mm/yyyy) !", vbCritical, "Data Error Boo"
txtTglLahir.SetFocus Exit Sub End If tglLahir = CDate(txtTglLahir.Text) Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() Dim usiaDetik As Long Dim usiaHari As Long Dim usiaTahun As Long
usiaDetik = DateDiff("s", tglLahir, Now) usiaHari = DateDiff("d", tglLahir, Now) usiaTahun = DateDiff("yyyy", tglLahir, Now)
lblDetik.Caption = CStr(usiaDetik) lblHari.Caption = CStr(usiaHari) lblTahun.Caption = CStr(usiaTahun) End Sub
+ Created: 28/08/2005
Menambahkan Graphic pada Form
Contoh membuat Tombol bergambar dengan menggunalan control image.
Buat empat image control dengan ukuran yang sama pada form kemudian stretch property jadikan nilai True. Berikan nama masing-masing keempat image tersebut dengan nama imgMain, imgMUp, imgMDown dan imgMMove serta berikan masing-masing tiga image (imgMUp, imgMDown, imgMMove) dengan gambar yang telah ditentukan pada property Picture dan berikan nilai False pada property Visible seperti di bawah ini:
Kemudian tuliskan kode seperti di bawah ini:
Private Sub Form_Load()
imgMain.Picture = imgMUp.Picture End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (X >= imgMain.Left) And (X <= (imgMain.Left + imgMain.Width)) And (Y >= imgMain.Top) And (Y <= (imgMain.Top + imgMain.Height)) Then
imgMain.Picture = imgMMove.Picture Else
imgMain.Picture = imgMUp.Picture End If
End Sub
Private Sub imgMain_Click() MsgBox "Hai, it's works....!" End Sub
Private Sub imgMain_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
imgMain.Picture = imgMDown.Picture End Sub
Private Sub imgMain_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
imgMain.Picture = imgMMove.Picture End Sub
Private Sub imgMain_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
imgMain.Picture = imgMUp.Picture End Sub
+ Created: 28/08/2005
Bekerja dengan File untuk menyimpan dan menerima data
Menyimpan data dengan statemen open:
Open FilePath [For Mode] [Access AcessType [LockType] As [#]FileNumber [Len=CharInBuffer%]
Tabel file Mode
Menerima data dengan statemen Input:
Line Input #FileHandle, strBuffer Contoh program:
Private Sub cmdClose_Click() End
End Sub
Private Sub cmdOpen_Click() Dim strNamaFile As String Dim fHandle As Integer Dim strBuffer As String Dim defMsPointer As Integer
dlgFile.DialogTitle = "Buka File Text" dlgFile.ShowOpen
If dlgFile.FileName <> "" Then strNamaFile = dlgFile.FileName
Mode Description
Append Menambah data pada file yang sudah ada, jika tidak ada maka file akan dibuat.
Binary Membuka file biner, jika tidak ada, file akan dibuat Input Membuka file untuk dibaca
Output Membuka file untuk ditulis, jika tidak ada, file akan dibuat Random Membuka file untuk akses acak, jika tidak ada, file akan dibuat.
+ Created: 28/08/2005
fHandle = FreeFile
Open strNamaFile For Input As #fHandle
defMsPointer = MousePointer MousePointer = vbHourglass txtEdit.Text = ""
Do While Not EOF(fHandle)
Line Input #fHandle, strBuffer
txtEdit.Text = txtEdit.Text & strBuffer & vbCrLf Loop
Close #fHandle
MousePointer = defMsPointer End If
End Sub
Private Sub cmdSave_Click() Dim strNamaFile As String Dim fHandle As Integer Dim strBuffer As String Dim defMsPointer As Integer
dlgFile.DialogTitle = "Simpan File Text" dlgFile.ShowSave
dlgFile.DefaultExt = "txt" If dlgFile.FileName <> "" Then strNamaFile = dlgFile.FileName fHandle = FreeFile
Open strNamaFile For Output As #fHandle
defMsPointer = MousePointer MousePointer = vbHourglass Print #fHandle, txtEdit.Text Close #fHandle
MousePointer = defMsPointer End If
End Sub
Private Sub Form_Load()
dlgFile.Filter = "File Text (*.txt)|*.txt|All Files (*.*)|*.*" End Sub
Bekerja dengan Object File System
Untuk mengaktifkan object tersebut, terlebih dahulu pilih menu References dari menu Project kemudian pilih Microsoft Scripting Runtime (SCRRUN.DLL).
+ Created: 28/08/2005 Table FileSystem Objects
Name Description
Drive Mengakses system Drive. Misalnya CD-ROM drives, RAM disks, atau mapped network drives.
Folder Mengakses direktori (folder).
File Membuka, membuat dan memindahkan file FileSystemObject Digunakan untuk mengakses object filesystem lain TextStream Membaca, menulis dan menambah file text Contoh Program:
Private Sub cmdBuka_Click() Dim strStat As String
Dim ofile As New FileSystemObject Dim fo As File strStat = "" dlgOpen.ShowOpen If dlgOpen.FileName <> "" Then Set fo = ofile.GetFile(dlgOpen.FileName)
strStat = "Nama File : " & fo.Name & vbCrLf strStat = strStat & "Path : " & fo.Path & vbCrLf
strStat = strStat & "Tgl. Dibuat : " & fo.DateCreated & vbCrLf
strStat = strStat & "Tgl. Dimodifikasi : " & fo.DateLastModified & vbCrLf
strStat = strStat & "Tgl. Diakses : " & fo.DateLastAccessed & vbCrLf strStat = strStat & "Size : " & fo.Size & " byte (Karakter)" & vbCrLf strStat = strStat & "Tipe : " & fo.Type & vbCrLf
strStat = strStat & "Parent : " & fo.ParentFolder & vbCrLf End If
txtStat.Text = strStat End Sub
+ Created: 28/08/2005