Fungsi MsgBox di dalam Visual Basic adalah untuk menampilkan pesan dalam suatu dialog box serta menunggu respon dari user yang membacanya. Umumnya dalam suatu aplikasi misalnya digunakan untuk menampilkan pesan peringatan dan sebagainya.
Syntax:
MsgBox(prompt[, buttons] [, title] [, helpfile, context])
prompt : suatu ekspresi string yang akan ditampilkan sebagai pesan dalam kotak dialog, maksimum panjang adalah 1024 karakter, tergantung dari lebar karakter yang digunakan
buttons : Optional, suatu ekspresi numerik dari sejumlah tombol, icon yang akan ditampilkan, default button adalah 0 yang akan menampilkan tombol OK
title : Optional, suatu ekspresi string yang akan ditampilkan sebagai judul dari kotak dialog, jika tidak diisi akan ditampilkan nama dari aplikasi sebagai judulnya
helpfile, context : Optional, hanya berfungsi apabila ada file help yang telah dibuat bersamaan dengan aplikasinya
Argumen buttons
First Group - Berdasarkan jenis tombol yang ditampilkan :
Constant | Value | Description |
vbOKOnly | 0 | Display OK button only. |
vbOKCancel | 1 | Display OK and Cancel buttons. |
vbAbortRetryIgnore | 2 | Display Abort, Retry, and Ignore buttons. |
vbYesNoCancel | 3 | Display Yes, No, and Cancel buttons. |
vbYesNo | 4 | Display Yes and No buttons. |
vbRetryCancel | 5 | Display Retry and Cancel buttons. |
Second Group - Berdasarkan jenis icon yang ditampilkan :
Constant | Value | Description | Icon |
vbCritical | 16 | Display Critical Message icon. | |
vbQuestion | 32 | Display Warning Query (question mark) icon. | |
vbExclamation | 48 | Display Warning Message icon. | |
vbInformation | 64 | Display Information Message icon. |
Third Group - Determines which button is the default:
Constant | Value | Description |
vbDefaultButton1 | 0 | First button is default. |
vbDefaultButton2 | 256 | Second button is default. |
vbDefaultButton3 | 512 | Third button is default. |
vbDefaultButton4 | 768 | Fourth button is default (applicable only if a Help button has been added). |
Fourth Group Determines the modality of the message box. Note generally, you would not need to use a constant from this group, as you would want to use the default (application modal). If you specified "system modal", you would be "hogging" Windows รข€“ i.e., if a user had another app open, like Word or Excel, they would not be able to get back to it until they responded to your app's message box.
Constant | Value | Description |
vbApplicationModal | 0 | Application modal; the user must respond to the message box before continuing work in the current application. |
vbSystemModal | 4096 | System modal; all applications are suspended until the user responds to the message box. |
There is a fifth group of constants that can be used for the buttons argument which would only be used under special circumstances:
Constant | Value | Description |
vbMsgBoxHelpButton | 16384 | Adds Help button to the message box |
VbMsgBoxSetForeground | 65536 | Specifies the message box window as the foreground window |
vbMsgBoxRight | 524288 | Text is right aligned |
vbMsgBoxRtlReading | 1048576 | Specifies text should appear as right-to-left reading on Hebrew and Arabic systems |
When you use MsgBox to with the option to display more than one button (i.e., from the first group, anything other than "vbOKOnly"), you can test which button the user clicked by comparing the return value of the Msgbox function with one of these values:
Constant | Value | Description |
vbOK | 1 | The OK button was pressed |
vbCancel | 2 | The Cancel button was pressed |
vbAbort | 3 | The Abort button was pressed |
vbRetry | 4 | The Retry button was pressed |
vbIgnore | 5 | The Ignore button was pressed |
vbYes | 6 | The Yes button was pressed |
vbNo | 7 | The No button was pressed |
Sumber : http://www.vb6.us/tutorials/understanding-msgbox-command-visual-basic
Output :
VB :
Dim i As Integer
For i = 1 To 10
MsgBox "Ini yang ke " & i, vbYes, "Pesan"
Next i
MsgBox "Terima Kasih", vbYes, "Pesan"
End Sub
Private Sub Command2_Click() 'Perintah untuk command button Keluar
Unload Form1
End Sub
0 Response to "Latihan Visual Basic (Message Box)"
Post a Comment