Maison  >  Article  >  développement back-end  >  Boîte de messages C#

Boîte de messages C#

WBOY
WBOYoriginal
2024-09-03 15:27:42579parcourir

Dans les applications d'aujourd'hui, il est toujours requis qu'un message soit affiché à l'utilisateur comme gage d'information ou de confirmation afin que l'utilisateur soit au courant de l'état de l'opération qu'il a effectuée. Le message peut être n'importe quoi allant de « Le paiement est réussi » ou un type d'avertissement comme « Voulez-vous continuer », etc. Ceci est réalisé en C# à l'aide de Message Box. Une boîte de message peut être considérée comme une interface entre l'utilisateur et l'application. Ce n'est rien d'autre qu'une fenêtre contenant du texte, des images ou des symboles pour guider ou transmettre quelque chose à l'utilisateur. Jusqu'à ce que l'action appropriée soit effectuée et que la boîte de message soit fermée, d'autres actions ne seront pas autorisées.

Syntaxe :

Message Box est une classe dans l'espace de noms « Systems.Windows.Forms » et l'assembly dans lequel elle est disponible est « System.Windows.Forms.dll ». La méthode show disponible dans la classe est utilisée pour afficher le message avec boutons d'action. Les boutons d'action peuvent aller de Oui à Non, Ok à Annuler.

Exemple :

Le code suivant créera une simple boîte de message uniquement avec le bouton OK.

string msg = "Test";
MessageBox.Show(msg);

Types de méthodes d'affichage

Voici les types de méthodes d'affichage :

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);
Syntaxe Utiliser MessageBox.Show(String) Il affichera uniquement la boîte de message avec la chaîne transmise. Un bouton ok est également présent pour fermer la boîte de dialogue.
Exemple : MessageBox.Show(String, String) Il affichera uniquement la boîte de message avec la chaîne passée en premier paramètre. Le deuxième paramètre est le titre de la boîte de message. Un bouton ok est également présent pour fermer la boîte de dialogue.
Exemple : MessageBox.Show( String,String, MessageBoxButtons) Il affichera la boîte de message avec le texte fourni, le titre et les boutons correspondants à afficher sur la boîte de message.
Par exemple, ci-dessous affichera les boutons Oui et Non. Afficher (String, String, MessageBoxButtons, MessageBoxIcon) Il affichera la boîte de message avec le texte fourni, le titre et les boutons correspondants à afficher sur la boîte de message. Il affichera également l'icône spécifiée avant le texte.
Par exemple, ci-dessous affichera les boutons Oui et Non avec un point d'interrogation devant le message. Afficher (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton) Il affichera la boîte de message avec le texte fourni, le titre et les boutons correspondants à afficher sur la boîte de message. Il affichera également l'icône spécifiée avant le texte. Le dernier paramètre indique quel bouton doit être sélectionné par défaut au chargement.
Par exemple, ci-dessous affichera les boutons Oui et Non avec un point d'interrogation devant le message. Afficher (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaulButton, MessageBoxOptions) Il affichera la boîte de message avec le texte fourni, le titre et les boutons correspondants à afficher sur la boîte de message. Il affichera également l'icône spécifiée avant le texte. Le dernier paramètre indique quel bouton doit être sélectionné par défaut au chargement et le contenu de la boîte de message sera aligné à droite.
Par exemple, ci-dessous affichera les boutons Oui et Non avec un point d'interrogation devant le message.

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:

Boîte de messages C#

Boîte de messages 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.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:Case à cocher en C#Article suivant:Case à cocher en C#