Start new project.
Add 6 buttons to the form.
Button1 text: Ok
Button2 text: Ok / Cancel
Button3 text: Abort / Retry / Ignore
Button4 text: Yes / No / Cancel
Button5 text: Yes / No
Button6 text: Retry / Cancel
The standard way of creating a message box is: msgbox("Some text"). Some text can be anything and it can also be empty but we must have the quotation marks inside the brackets. Also we can insert a variable inside the message box. The displayed text is the first parameter of the message box and it's the required one. It can have 2 more parameters which are obviously optional. We divide the parameters with commas.
- 1. parameter: text content of the message box
- 2. parameter: the type of the message box
- 3. parameter: the title of the message box
Double click on the first button, insert this:
MsgBox("Press Ok", MsgBoxStyle.OkOnly, "This is an OK only box")
Instead of writing MsgBoxStyle.OkOnly we could use the visual basic constant for the OkOnly button style: vbOKOnly. Also we can use an integer value: 0.
In fact, we could say that the second parameter actually works with integer numbers, but we can use the textual names of the settings and constants that hold those integer values.
Button Style Constants Value
MsgBoxStyle.OkOnly vbOKOnly 0
MsgBoxStyle.OkCancel vbOKCancel 1
MsgBoxStyle.AbortRetryIgnore vbAbortRetryIgnore 2
MsgBoxStyle.YesNoCancel vbYesNoCancel 3
MsgBoxStyle.YesNo vbYesNo 4
MsgBoxStyle.RetryCancel vbRetryCancel 5
Declare this below the button1 sub:
Dim aa
Go to the design page, double click on the second button, add this:
MsgBox("Choose between Ok and Cancel", MsgBoxStyle.OkCancel, "OK - Cancel box")
- start program, click on button2. We can store the returned value of the message box (the clicked button) into a variable. Turn the previous line into this:
aa = MsgBox("Choose between Ok and Cancel", MsgBoxStyle.OkCancel, "OK - Cancel box")
MsgBox(aa)
- choosing Ok should give you 1, while cancel should be 2.
Button Name Returned Value
OK 1
Cancel 2
Abort 3
Retry 4
Ignore 5
Yes 6
No 7
-> for constants just attach "vb" at the front of the name
Double click on the third button, add this to the sub:
MsgBox("Choose: Abort, Retry or Ignore", MsgBoxStyle.AbortRetryIgnore, "Abort - Retry - Ignore choice")
- press the button.
Let's make some changes to the previous line:
MsgBoxStyle.Critical - this will add an icon to the message box, it also comes with a notification sound.
After you write: DialogResult. -> you will be given a menu with all the possible buttons you can press, so you can then use "if" statements to give each possible choice a desired task
DialogResult.(Button) - it stores the same integer value that is also returned by the message box when a button has been chosen. Try this: MsgBox(DialogResult.Cancel)
Button Icon Constants Value
MsgBoxStyle.Critical vbCritical 16
MsgBoxStyle.Question vbQuestion 32
MsgBoxStyle.Exclamation vbExclamation 48
MsgBoxStyle.Information vbInformation 64
Double click on the fourth button, add this inside the sub:
- this time we have used constants in the first message box.
The "if" statement compares the value of the starting message box to the constants of the buttons that were used, like "vbYes".
vbInformation + vbYesNoCancel - the value of "vbinformation" is 64, while the value of "vbYesNoCancel" is 3, so their sum is 67.
Let's only use numerical values in the fourth button:
- everything should be the same as before.
If you were to use something like vbOkCancel + vbAbort as the second parameter, you would just be adding together 1 + 3 and giving the message box: 4, so you would get a Yes / No message box (because it's integer value is 4).
Double click on the fifth button, add this:
MsgBox("Waiting for Yes or No", vbOKCancel + vbAbort + 32 + 256, "The Wait: Yes - No")
- 32 will give us the "MsgBoxStyle.Question" icon, while 256 makes the second button the default one, which means the second button will be highlighted (focused on) when the message box appears. By default, the button most left is the one selected.
Setting Constant Value
MsgBoxStyle.DefaultButton1 vbDefaultButton1 0
MsgBoxStyle.DefaultButton2 vbDefaultButton2 256
MsgBoxStyle.DefaultButton3 vbDefaultButton3 512
Double click the last button, add inside:
- "MessageBox.Show" must be used a little bit differently, you have to separate each type of message box setting with a comma, you can't add them together.
MessageBoxIcon - might offer you more icon settings than "MsgBoxStyle" did, but there is still only 4 different icons you can put in the message. "MessageBoxIcon" has repetition, for example, "error", "hand" and "stop" are exactly the same, they give you the "Critical" icon
Final code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Press Ok", MsgBoxStyle.RetryCancel, "This is an OK box")
End Sub
Dim aa
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
aa = MsgBox("Choose between Ok and Cancel", MsgBoxStyle.OkCancel, "OK - Cancel box")
MsgBox(aa)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If MsgBox("Choose: Abort, Retry or Ignore", MsgBoxStyle.Critical + MsgBoxStyle.AbortRetryIgnore, "Abort - Retry - Ignore choice") = DialogResult.Abort Then
MsgBox("Abort was selected")
Else
MsgBox("You didn't abort, good for you")
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
aa = MsgBox("Your choice: Yes, No or Cancel?", 67, "Yes - No - Cancel message")
If aa = 6 Then
MsgBox("yes was chosen", , "YES title")
ElseIf aa = 7 Then
MsgBox("No has been selected", vbOKOnly, )
Else
MsgBox("Canceled", 0, 0)
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
MsgBox("Waiting for Yes or No", vbOKCancel + vbAbort + 32 + 256, "The Wait: Yes - No")
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
If MessageBox.Show("Box Name", "Box Title", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) = vbRetry Then
MsgBox("Retry selected")
End If
End Sub
End Class