This Season
 

Visual Basic Tutorial: Msgbox

Visual Basic Tutorial: Msgboxthumbnail
MsgBox

The MsgBox function exists in both Visual Basic 6.0 and Visual Basic.NET. In both versions, it provides a simple way for the programmer to display a text message and include specific options for user response. These options include buttons that read "OK" or "Yes," "Cancel," "AbortRetryIgnore" as well as icons for warnings or information. The MsgBox can also display a link to a help file.

Related Searches:
    1. Form Example

      • MsgBox Form Demo

        Writing a very short Visual Basic program demonstrates the use of the MsgBox function. The example here shows how the programmer can cause a variety of actions to occur depending on a user's response. Invoking the MsgBox can deal with critical errors, simply alert the user to something, or let the person decide whether to continue.
        To see this in action, open the Visual Basic 6.0 or Visual Basic.NET software. In Visual Basic, create a new program by clicking on "File." Select "New project" from the drop down list and a "Standard EXE" from the template list. In Visual Basic.NET or Visual Basic 2008 Express Edition, create a new project the same way but choose the "Windows Forms Application"
        Create one Command Button and three Labels on the form by clicking and dragging these tools from the Toolbox. Click on each label in the Properties box on the right while using the Design view and delete the Caption text. Just to make the example a little more user friendly, change the Command button caption in the Properties list to "Show Me." The form should look like the image here.

      Code Example

      • MsgBox Code

        In Visual Basic 6 click on "View" in the menu and choose "Code" from the drop down list. Type the following commands exactly as shown below. In Visual Basic.NET, pressing "F7" will bring up the code view. NOTE: The Visual Basic interface automatically inserts tabs on some of these lines that do not display properly in this article. Do not worry about this. The included image shows the way the coding will appear in the software.

        Private Sub Command1_Click()
        Dim Msg, Style, Title, Response
        Msg = "Do you want to continue ?"
        Style = vbAbortRetryIgnore + vbCritical + vbDefaultButton2 ' Define buttons.
        Title = "MsgBox Demonstration"
        Response = MsgBox(Msg, Style, Title)
        If Response = vbAbort Then
        Label1.Caption = "You shouldn't have done that!"
        ElseIf Response = vbRetry Then
        Label2.Caption = "Retry all you want; Nothing will work!" ' Perform some action.
        Else: Label3.Caption = "That's the right answer!"
        MsgBox ("Press any key to end this")
        If vbOK Then
        End
        End If
        End If
        End Sub

        Pressing "F5" in either version will run the program and demonstrate the MsgBox as created. Depending on the needs of the application, instead of having messages appear, the program can have actions occur such as opening new windows, triggering error correction procedures, shutting down the program or a number of things that are possible within the program.

    Related Searches

    References

    Resources

    Read Next:

    Comments

    You May Also Like

    Follow eHow

    Related Ads