MsgBox (Kotak Pesan)
Berfungsi untuk menampilkan pesan atau komentar dalam bentuk form.
Bentuk Perintah
MsgBox(“Isi Pesan“, MsgBoxStyle, “Judul Pesan“)
Keterangan
gambar:
- Judul Pesan
- Msgboxstyle
- Isi Pesan
Ada beberapa MsgBoxStyle, yaitu:
- MsgBoxStyle.Critical(16) : Tombol Ok dan gambar icon Critical
- MsgBoxStyle.Question(32) : Tombol Ok dan gambar icon Question
- MsgBoxStyle.Exclamation(48) : Tombol Ok dan gambar icon Exclamation
- MsgBoxStyle.Information(64) : Tombol Ok dan gambar icon Information
- MsgBoxStyle.AbortRetryIgnore(2) : Tombol Abourt,Retry dan Ignore
- MsgBoxStyle.OkCancel(1) : Tombol OK dan Cancel
- MsgBoxStyle.Ok Only(0) : Tombol OK
- MsgBoxStyle.RetryCancel(5) : Tombol Retry dan Cancel
- MsgBoxStyle.YesNo(4) : Tombol Yes dan No
- MsgBoxStyle.YesNoCancel(3) : Tombol Yes,No dan Cancel
Contoh 1:
Membuat Form Login dengan tampilan sbb:
Alur
Program:
- Input Password.
- Klik Proses
- Jika Password benar maka akan tampil kotak
pesan (MessageBox) dengan tampilan sbb:
· Isi
pesan : Password yang Anda Masukkan Benar
· Judul
Pesan : Info
Login
· MsgBoxStyle : Tombol
OK dengan gambar icon Information.
- Jika Password salah maka akan tampil kotak
pesan (MessageBox) dengan tampilan sbb:
· Isi
pesan : Password yang Anda Masukkan Salah”
· Judul
Pesan : Info
Login
· MsgBoxStyle : Tombol
OK dengan gambar icon Critical.
Kode Program:
Kode
program diketik pada Objek BtnProses (Tombol Proses) sbb:
Private Sub BtnProses_Click(ByVal sender As
System.Object, ...
If TxtPas.Text = "rahasia"
Then
MsgBox("Password yang Anda Masukkan Benar",
MsgBoxStyle.Information,
"Info Login")
Else
MsgBox("Password yang Anda Masukkan Salah", 16,
"Info")
End If
End Sub
Private Sub txtpas_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
txtpas.KeyPress
If e.KeyChar = Chr(13) Then
'btnproses.Focus()
btnproses_Click(sender,NewSystem.EventArgs())
End If
End Sub
Contoh 2:
Tambah tombol Tutup pada form Login, sehingga tampilan
form menjadi seperti berikut:
Buat Perintah untuk Tombol Tutup menggunakan MessageBox
dengan tampilan sbb:
- Isi Pesan : “Apakah
Anda ingin menutup Form Login ini?“
- MsgBoxStyle : Yes dan No dengan icon Question.
- Judul : “Tutup
Form Login“
Alur Program:
Jika diklik tombol Yes, maka Program akan ditutup. Jika
diklik tombol No, maka Program tidak akan ditutup.
Perintah Program:
Private Sub BtnTutup_Click(ByVal sender As
System.Object, ...
Dim pesan As String
pesan = MsgBox("Apakah Anda ingin menutup Program ini?",
MsgBoxStyle.YesNo + 32, "Tutup Program")
If pesan = vbYes Then
End
Else
Exit Sub
End If
End Sub