MessageBox 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
사용자에게 메시지를 표시하는 대화 상자라고도 하는 메시지 창을 표시합니다. 사용자가 닫을 때까지 애플리케이션의 다른 작업을 차단하는 모달 창입니다. A MessageBox 는 사용자에게 알리고 지시하는 텍스트, 단추 및 기호를 포함할 수 있습니다.
public ref class MessageBox
public class MessageBox
type MessageBox = class
Public Class MessageBox
- 상속
-
MessageBox
예제
다음 코드 예제에서는 a를 사용하여 MessageBox 사용자에게 누락된 항목을 TextBox알리는 방법을 보여줍니다. 이 예제에서는 명명된 ServerName이 있는 기존 양식에서 메서드를 TextBox 호출해야 합니다.
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 새 인스턴스를 만들 수 없습니다. 메시지 상자를 표시하려면 메서드MessageBox.Show를 호출합니다static. 메시지 상자에 표시되는 제목, 메시지, 단추 및 아이콘은 이 메서드에 전달하는 매개 변수에 따라 결정됩니다.