MessageBox 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示消息窗口(也称为对话框),向用户显示消息。 这是一个模式窗口,可阻止应用程序中的其他操作,直到用户将其关闭。 MessageBox 可包含通知并指示用户的文本、按钮和符号。
public ref class MessageBox
public class MessageBox
type MessageBox = class
Public Class MessageBox
- 继承
-
MessageBox
示例
下面的代码示例演示如何使用 a MessageBox 来通知用户缺少的 TextBox条目。 此示例要求从具有 TextBox 命名 ServerName 的现有窗体调用该方法。
private:
void validateUserEntry()
{
// Checks the value of the text.
if ( serverName->Text->Length == 0 )
{
// Initializes the variables to pass to the MessageBox::Show method.
String^ message = "You did not enter a server name. Cancel this operation?";
String^ caption = "No Server Name Specified";
MessageBoxButtons buttons = MessageBoxButtons::YesNo;
System::Windows::Forms::DialogResult result;
// Displays the MessageBox.
result = MessageBox::Show( this, message, caption, buttons );
if ( result == ::DialogResult::Yes )
{
// Closes the parent form.
this->Close();
}
}
}
private void validateUserEntry()
{
// Checks the value of the text.
if(serverName.Text.Length == 0)
{
// Initializes the variables to pass to the MessageBox.Show method.
string message = "You did not enter a server name. Cancel this operation?";
string caption = "Error Detected in Input";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(message, caption, buttons);
if (result == System.Windows.Forms.DialogResult.Yes)
{
// Closes the parent form.
this.Close();
}
}
}
Private Sub ValidateUserEntry()
' Checks the value of the text.
If ServerName.Text.Length = 0 Then
' Initializes variables to pass to the MessageBox.Show method.
Dim Message As String = "You did not enter a server name. Cancel this operation?"
Dim Caption As String = "Error Detected in Input"
Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
Dim Result As DialogResult
'Displays the MessageBox
Result = MessageBox.Show(Message, Caption, Buttons)
' Gets the result of the MessageBox display.
If Result = System.Windows.Forms.DialogResult.Yes Then
' Closes the parent form.
Me.Close()
End If
End If
End Sub
下面的代码示例演示如何向用户询问“是”或“否”,并根据响应做出决定。
private:
void Form1_FormClosing(Object^ sender, FormClosingEventArgs^ e)
{
// If the no button was pressed ...
if ((MessageBox::Show(
"Are you sure that you would like to close the form?",
"Form Closing", MessageBoxButtons::YesNo,
MessageBoxIcon::Question) == DialogResult::No))
{
// cancel the closure of the form.
e->Cancel = true;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
const string message =
"Are you sure that you would like to close the form?";
const string caption = "Form Closing";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
// If the no button was pressed ...
if (result == DialogResult.No)
{
// cancel the closure of the form.
e.Cancel = true;
}
}
Private Sub Form1_FormClosing( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
Dim message As String = _
"Are you sure that you would like to close the form?"
Dim caption As String = "Form Closing"
Dim result = MessageBox.Show(message, caption, _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question)
' If the no button was pressed ...
If (result = DialogResult.No) Then
' cancel the closure of the form.
e.Cancel = True
End If
End Sub
注解
不能创建新类实例 MessageBox 。 若要显示消息框,请调用 static
方法 MessageBox.Show。 消息框中显示的标题、消息、按钮和图标由传递给此方法的参数确定。