C# メッセージボックス

WBOY
リリース: 2024-09-03 15:27:42
オリジナル
579 人が閲覧しました

今日のアプリケーションでは、ユーザーが実行した操作のステータスを認識できるように、情報または確認のトークンとしてメッセージをユーザーに表示することが常に必要です。メッセージには、「支払いは成功しました」から「続行しますか」などの警告タイプまで、さまざまなものを指定できます。これは、メッセージ ボックスを使用して C# で実現されます。メッセージ ボックスは、ユーザーとアプリケーション間のインターフェイスと考えることができます。これは、ユーザーに何かを案内したり伝えたりするためのテキスト、画像、または記号を含むウィンドウにすぎません。適切なアクションが実行され、メッセージ ボックスが閉じられるまで、他のアクションは実行できません。

構文:

メッセージ ボックスは「Systems.Windows.Forms」名前空間のクラスであり、利用できるアセンブリは「System.Windows.Forms.dll」です。クラスで利用できる show メソッドは、メッセージとともにメッセージを表示するために使用されます。アクションボタン。アクション ボタンには、[はい] から [いいえ]、[OK] から [キャンセル] までの範囲のものを使用できます。

例:

次のコードは、[OK] ボタンのみを使用して単純なメッセージ ボックスを作成します。

string msg = "Test";
MessageBox.Show(msg);
ログイン後にコピー

表示メソッドの種類

show メソッドの種類は次のとおりです:

Syntax Use
MessageBox.Show(String) It will display only the message box with the string that is passed. An ok button is also present to close the dialog.
Example:
Messagebox.Show("Test")
ログイン後にコピー
MessageBox.Show( String, String) It will display only the message box with the string that is passed as first parameter. The second parameter is the title of the Message Box. An ok button is also present to close the dialog.
Example:
MessageBox.Show( “Message”, ”Title”).
ログイン後にコピー
MessageBox.Show( String,String, MessageBoxButtons) It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box.
For eg the below will display Yes and No buttons.
MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo);
ログイン後にコピー
Show(String, String, MessageBoxButtons, MessageBoxIcon) It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text.
For eg the below will display Yes and No buttons with a question mark in front of message.
MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
ログイン後にコピー
Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton) It will display the message box with the supplied text, title and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text. The last parameter denotes which button must be selected by default on load.
For eg the below will display Yes and No buttons with a question mark in front of message.
MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
ログイン後にコピー
Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton, MessageBoxOptions) It will display the message box with the supplied text, title, and the corresponding buttons to be displayed on the Message Box. It will also display the icon that is specified before the text. The last parameter denotes which button must be selected by default on load and the contents of the messagebox will be right-aligned.
For eg the below will display Yes and No buttons with a question mark in front of message.
MessageBox.Show( "Message”, "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MesageBoxOptions.RightAlign, true);
ログイン後にコピー
構文 使用 MessageBox.Show(String) 渡された文字列を含むメッセージ ボックスのみが表示されます。ダイアログを閉じるための [ok] ボタンも表示されます。
例: MessageBox.Show( String, String) 最初のパラメータとして渡された文字列を含むメッセージ ボックスのみが表示されます。 2 番目のパラメータはメッセージ ボックスのタイトルです。ダイアログを閉じるための [ok] ボタンも表示されます。
例: MessageBox.Show( String,String, MessageBoxButtons) 指定されたテキスト、タイトル、およびメッセージ ボックスに表示される対応するボタンを含むメッセージ ボックスが表示されます。
たとえば、以下では [はい] ボタンと [いいえ] ボタンが表示されます。 Show(String, String, MessageBoxButtons, MessageBoxIcon) 指定されたテキスト、タイトル、およびメッセージ ボックスに表示される対応するボタンを含むメッセージ ボックスが表示されます。テキストの前に指定したアイコンも表示され
ます。 たとえば、以下ではメッセージの前に疑問符が付いた [はい] ボタンと [いいえ] ボタンが表示されます。 Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton) 指定されたテキスト、タイトル、およびメッセージ ボックスに表示される対応するボタンを含むメッセージ ボックスが表示されます。テキストの前に指定されたアイコンも表示されます。最後のパラメータは、読み込み時にデフォルトでどのボタンを選択する必要があるかを示します。
たとえば、以下ではメッセージの前に疑問符が付いた [はい] ボタンと [いいえ] ボタンが表示されます。 Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton, MessageBoxOptions) 指定されたテキスト、タイトル、およびメッセージ ボックスに表示される対応するボタンを含むメッセージ ボックスが表示されます。テキストの前に指定されたアイコンも表示されます。最後のパラメータは、読み込み時にデフォルトでどのボタンを選択する必要があるかを示し、メッセージボックスの内容は右揃えになります。
たとえば、以下ではメッセージの前に疑問符が付いた [はい] ボタンと [いいえ] ボタンが表示されます。 テーブル>

Types of MessageBox Buttons

The following are the types of Buttons that are available in the MessageBox.Show() method. They are

  • OK: It is defined as MessageBoxButtons.OK
  • OK and Cancel: It is defined as MessageBoxButtons.OkCancel.
  • Abort Retry and Ignore: It is defined as MessageBoxButtons.AbortRetryIgnore.
  • Yes No and Cancel: It is defined as MessageBoxButtons.YesNoCancel.
  • Yes and No: It is defined as MessageBoxButtons.YesNo.
  • Retry and Cancel: It is defined as MessageBoxButtons.RetryCancel.

Types of MessageBox Icons

The following are the types of MessageBox icons method are:

  • None: No icons are displayed in the Message box.
  • Hand: A hand icon is displayed. It is defined as MessageBoxIcon.Hand.
  • Question: A question mark is displayed. It is defined as MessageBoxIcon.Question.
  • Exclamation: An exclamation mark is displayed. It is defined as MessageBoxIcon.Exclamation.
  • Asterisk: An asterisk symbol is displayed. It is defined as MessageBoxIcon.Asterisk.
  • Stop: A stop icon is displayed. It is defined as MessageBoxIcon.Stop.
  • Error: An error icon is displayed. It is defined as MessageBoxIcon.Error.
  • Warning: A warning icon is displayed. It is defined as MessageBoxIcon.Warning.
  • Information: An info symbol is displayed. It is defined as MessageBoxIcon.Information.

Types of MessageBox Options

The following are the various Message Box options that are available.

  • ServiceNotification: It is defined as MessageBoxOptions.ServiceNotification. This is used to display the message box on the current desktop which is active. The message box is displayed even when no user is logged on to the desktop.
  • DefaultDesktopOnly: It is defined as MessageBoxOptions.DefaultDesktopOnly. This also displays on the currently active desktop. The difference between this and service notification is that here the message is displayed on the interactive window.
  • RightAlign: It is defined as MessageBoxOptions.RightAlign. This is used to format the message in right alignment.
  • RtlReading: It is defined as MessageBoxOptions.RtlReading. This denotes that message is displayed from right to left order.

Example of C# MessageBox

Following are the examples of c# message box are:

Input:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class testform : Form
{
public testform()
{
InitializeComponent();
}
private void testform_Load(object sender, EventArgs e)
{
MessageBox.Show("Demo of MsgBox");
MessageBox.Show("Demo of MsgBox", "Title");
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNo);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OkCancel);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.RetryCancel);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OK);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.AbortRetryIgnore);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Hand);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Exclamation);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Asterisk);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Stop);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Error);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Information);MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
MessageBox.Show("Demo of Msgbox","Title",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
}
}
ログイン後にコピー

Output:

C# メッセージボックス

C# メッセージボックス

Conclusion – C# MessageBox

Thus, the article covered in detail about the Message box class in c# in detail. It explained about various message box show methods that are available, the various parameters of each method, and demonstrated that with an example. The article also covered in detail about various message box options, message box buttons, and message box icons in detail along with their use. To learn more in detail it is advisable to write sample programs and practice them.

以上がC# メッセージボックスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!