How to Create Message Box Pop Ups in VB.NET
Creating different types of messages boxes in Visual Basic is a simple one line statement. I’m going to run through how to create several different types of message boxes in this tutorial, including OK Only, OK/Cancel, Abort/Retry/Ignore, and a Critical message box.
The simplest message box, it only includes an OK button…
1 | MsgBox("Hello World!") |
Ok, so the title of the message box the name of the project, let’s add our own title as the third argument, but first we’ll need to tell VB that we want to have this message box act as an OK only.
1 | MsgBox("Hello World!", MsgBoxStyle.OkOnly, "www.brangle.com") |
The next message box is the OK/Cancel with a custom title.
1 | MsgBox("Visit brangle.com?", MsgBoxStyle.OkCancel, "www.brangle.com") |
OK great, but this time lets detect whether the user clicked OK or Cancel. If the user clicks OK, We’ll launch my website 🙂 otherwise we’ll do nothing.
1 2 3 | If MsgBox("Visit brangle.com?", MsgBoxStyle.OkCancel, "www.brangle.com") = MsgBoxResult.Ok Then Process.Start("http://www.brangle.com/") End If |
Let’s try using an Abort/Retry/Ignore message box.
1 2 3 4 5 6 7 8 9 10 11 12 | Dim result As DialogResult result = MsgBox("Corrupt File. Try Again?", MsgBoxStyle.AbortRetryIgnore, "www.brangle.com") If result = Windows.Forms.DialogResult.Abort Then 'Do some action to abort opening the file MsgBox("You clicked abort") ElseIf result = Windows.Forms.DialogResult.Retry Then 'Do some action to retry opening the file MsgBox("You clicked retry") ElseIf result = Windows.Forms.DialogResult.Ignore Then 'Do some action to ignore MsgBox("You clicked ignore") End If |
Now let’s launch a message box as critical, this will display the red icon with x thru it
1 | MsgBox("File not found!", MsgBoxStyle.Critical, "Error") |