How to Create a Message Box in Visual Basic
Message boxes are Windows components that display some kind of dialog with the user. A message box is used to give the user an alert, ask for a "yes" or "no" response, or simply provide additional information when the user makes a selection. A message box has a predefined function in Visual Basic. You can use the "MsgBox()" function in Visual Basic to supply a popup window for the user.
Instructions
-
-
1
Create a string variable to hold your message. The MsgBox() function displays text that you define regardless of the type of message. The following code creates a string with an alert for the user:
Dim strMessage as String
strMessage = "Are you sure you want to delete this object?" -
2
Create a title for the message box. When you receive a message box, the top title bar displays a title for the window. The string below is used to set a title for the window:
Dim strTitle as String
strTitle = "Application Confirmation" -
-
3
Set a style for the message box. Styles include a "Yes/No" confirmation box, a box that just displays an "Ok" button or a box that gives the user the ability to abort an action. For this example, the code below sets the style for a "Yes/No" message box.
Dim style as MsgBoxStyle
style = MsgBoxStyle.YesNo -
4
Display the message box for the user. Combining all the options created in the previous steps, you can now display the message box and retrieve the user's answer. The code below displays the message box:
Dim response as Integer
response = MsgBox(strMessage, style, strTitle)
-
1
References
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images