Export (0) Print
Expand All
27 out of 54 rated this helpful - Rate this topic

MessageBox Class

Displays a message box that can contain text, buttons, and symbols that inform and instruct the user.

For a list of all members of this type, see MessageBox Members.

System.Object
   System.Windows.Forms.MessageBox

[Visual Basic]
Public Class MessageBox
[C#]
public class MessageBox
[C++]
public __gc class MessageBox
[JScript]
public class MessageBox

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You cannot create a new instance of the MessageBox class. To display a message box, call the static (Shared in Visual Basic) method MessageBox.Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.

Example

[Visual Basic, C#, C++] The following example shows how to use a MessageBox to inform the user of a missing entry in a TextBox. This example assumes that the method is called from an existing form with a Button and a TextBox on it.

[Visual Basic] 
Protected Sub button1_Click(sender As Object, e As System.EventArgs)
    If textBox1.Text = "" Then
        MessageBox.Show("You must enter a name.", "Name Entry Error", _
           MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        ' Code to act on the data entered would go here.
    End If
End Sub

[C#] 
protected void button1_Click(object sender, System.EventArgs e) {
   if(textBox1.Text == "") {
      MessageBox.Show("You must enter a name.", "Name Entry Error",
         MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
   }
   else {
      // Code to act on the data entered would go here.
   }
}

[C++] 
protected:
 void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    if(textBox1->Text->Equals(S"")) {
       MessageBox::Show(S"You must enter a name.", S"Name Entry Error",
          MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
    }
    else {
       // Code to act on the data entered would go here.
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

MessageBox Members | System.Windows.Forms Namespace | Show

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.