在現今的應用程式中,總是需要向使用者顯示一則訊息作為資訊或確認的標記,以便使用者了解他所執行的操作的狀態。該訊息可以是任何內容,從「付款成功」到「您想繼續嗎」等警告類型。這是在 C# 中藉助 Message Box 實現的。訊息框可以被視為使用者和應用程式之間的介面。它只不過是一個帶有文字、圖像或符號的窗口,用於指導或向使用者傳達某些內容。在執行適當的操作並關閉訊息框之前,不允許執行其他操作。
文法:
訊息方塊是「Systems.Windows.Forms」命名空間中的一個類,它可用的組件是「System.Windows.Forms.dll」。該類別中可用的 show 方法用於顯示訊息以及操作按鈕。操作按鈕可以是從「是」到「否」、「確定」到「取消」的任何內容。
範例:
以下程式碼將建立一個僅帶有「確定」按鈕的簡單訊息框。
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); 登入後複製 |
The following are the types of Buttons that are available in the MessageBox.Show() method. They are
The following are the types of MessageBox icons method are:
The following are the various Message Box options that are available.
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:
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中文網其他相關文章!