MessageBox クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


MessageBox クラスの新しいインスタンスは作成できません。メッセージ ボックスを表示するには、static メソッドの MessageBox.Show を呼び出します。メッセージ ボックスに表示されるタイトル、メッセージ、ボタン、およびアイコンは、このメソッドに渡すパラメータによって決定されます。

MessageBox を使用して、TextBox にデータが入力されていないことをユーザーに通知する方法を次のコード例に示します。この例では、このメソッドを Button および TextBox が配置された既存のフォームから呼び出す必要があります。
Private 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
private 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. } }
private: void button1_Click( Object^ sender, System::EventArgs^ e ) { if ( textBox1->Text->Equals( "" ) ) { 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. } }
protected void button1_Click(Object sender, System.EventArgs e) { if (textBox1.get_Text().Equals("")) { 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. } } //button1_Click
はい、またはいいえで答える質問をユーザーに提示し、その回答に基づいて決定を行う方法を次のコード例に示します。
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 = DialogResult.Yes Then ' Closes the parent form. Me.Close() End If End If End Sub
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 == 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 = "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.get_Text().get_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; DialogResult result; // Displays the MessageBox. result = MessageBox.Show(this, message, caption, buttons); if (result.Equals(DialogResult.Yes)) { // Closes the parent form. this.Close(); } } } //ValidateUserEntry

System.Windows.Forms.MessageBox


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- MessageBox クラスのページへのリンク