首頁  >  文章  >  後端開發  >  C# 訊息框

C# 訊息框

WBOY
WBOY原創
2024-09-03 15:27:42790瀏覽

在現今的應用程式中,總是需要向使用者顯示一則訊息作為資訊或確認的標記,以便使用者了解他所執行的操作的狀態。該訊息可以是任何內容,從「付款成功」到「您想繼續嗎」等警告類型。這是在 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);
語法 使用 MessageBox.Show(字串) 它將只顯示帶有傳遞的字串的訊息框。也存在一個「確定」按鈕來關閉對話框。
例: MessageBox.Show(字串, 字串) 它將僅顯示帶有作為第一個參數傳遞的字串的訊息框。第二個參數是訊息框的標題。也存在一個「確定」按鈕來關閉對話框。
例: MessageBox.Show(字串,字串,MessageBoxButtons) 它將顯示訊息框,其中包含提供的文字、標題以及要在訊息框中顯示的相應按鈕。
例如,下面將顯示“是”和“否”按鈕。 顯示(字串、字串、MessageBoxButtons、MessageBoxIcon) 它將顯示訊息框,其中包含提供的文字、標題以及要在訊息框中顯示的相應按鈕。它還會顯示在文字之前指定的圖示。
例如,下面將顯示“是”和“否”按鈕,訊息前面帶有問號。 顯示(字串、字串、MessageBoxButtons、MessageBoxIcon、MessageBoxDefaulButton) 它將顯示訊息框,其中包含提供的文字、標題以及要在訊息框中顯示的相應按鈕。它也會在文字之前顯示指定的圖示。最後一個參數表示載入時必須預設選擇哪個按鈕。
例如,下面將顯示“是”和“否”按鈕,訊息前面帶有問號。 顯示(字串、字串、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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 中的複選框下一篇:C# 中的複選框