Rumah >pembangunan bahagian belakang >Tutorial C#.Net >C# MessageBox
Dalam aplikasi hari ini, mesej sentiasa perlu dipaparkan kepada pengguna sebagai tanda maklumat atau pengesahan supaya pengguna mengetahui status operasi yang dilakukannya. Mesej boleh terdiri daripada apa sahaja daripada "Pembayaran berjaya" atau jenis amaran seperti "Adakah anda mahu meneruskan" dsb. Ini dicapai dalam C# dengan bantuan Kotak Mesej. Kotak mesej boleh dianggap sebagai antara muka antara pengguna dan aplikasi. Ia tidak lain hanyalah tetingkap yang mempunyai teks, imej atau simbol untuk membimbing atau menyampaikan sesuatu kepada pengguna. Sehingga tindakan yang sesuai dilakukan dan kotak mesej ditutup, ia tidak akan membenarkan tindakan lain dilakukan.
Sintaks:
Kotak Mesej ialah kelas dalam Ruang Nama "Systems.Windows.Forms" dan pemasangan yang tersedia ialah "System.Windows.Forms.dll".Kaedah paparan yang tersedia dalam kelas digunakan untuk memaparkan mesej bersama-sama dengan butang tindakan. Butang tindakan boleh terdiri daripada apa sahaja daripada Ya hingga Tidak, Ok hingga Batal.
Contoh:
Kod berikut akan mencipta Kotak Mesej ringkas hanya dengan butang OK.
string msg = "Test"; MessageBox.Show(msg);
Berikut ialah jenis kaedah persembahan:
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.
Atas ialah kandungan terperinci C# MessageBox. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!