C# explanation of message dialog box based on MessageBox class

preface:

In learning Windows application development, we often use the message dialog box to give users or administrators some message prompts. They are all based on an application of the message dialog box of the MessageBox class. In C#, the MessageBox message dialog box is located in the namespace System.Windows.Forms. Generally, a message dialog box contains information prompt text content, message dialog box title text, user response button and information icon content. We can set the message dialog box according to our own needs. Well, let's start learning!!!!

Once a day, happy all day

1. Create a form file

Note: try not to take the same file name or project name as the variable name of the system. For example, the MessageBox taken by the blogger's file name has to be changed later

1.1 design interface

2. Recognize the properties and icons of the message dialog box

  AbortRetryIgnore	  In the message box dialog box, there are three buttons: abort, retry and ignore
  OK	  Provide the OK button in the message box dialog box
  OKCancel	  Click OK and cancel in the message box dialog box
  RetryCancel	  Retry and Cancel buttons are provided in the message box dialog box
  YesNo	  Yes and no buttons are provided in the message box dialog box
  YesNoCancel	  In the message box dialog box, there are three buttons: Yes, no and cancel

3. Implementation effect of MessageBox message dialog box

3.1 AbortRetryIgnore effect display

The message prompt box type uses AbortRetryIgnore to provide three buttons in the message box dialog box: abort, retry and ignore. The icon is set to Warning and the Warning icon

3.2 OK effect display

The message prompt box type uses OK. The "OK" button is provided in the message box dialog box. The icon is set to Asterisk and the message icon

3.3 okancel effect display

The message prompt box type uses OKCancel to provide "OK" and "Cancel" buttons in the message box dialog box. The icon is set to Error and the Error warning icon

3.4 RetryCancel effect display

The message prompt box type uses RetryCancel to provide "retry" and "Cancel" buttons in the message box dialog box. The icon is set to Question and the Question mark system icon

3.5 YesNo effect display

The message prompt box type uses YesNo. Yes and no buttons are provided in the message box dialog box. The icon is set to Question and the Question mark system icon

3.6 YesNoCancel effect display

The message prompt box type uses YesNoCancel to provide three buttons "yes", "no" and "Cancel" in the message box dialog box, and the icon is None Blank Icon

4. Code display

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestMessageBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box. The message dialog box provides three buttons: abort, retry and ignore!", "Test test", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);//The first parameter text indicates the prompt content, the second parameter text indicates the message box title, the third parameter MessageBoxButtons indicates the button style of the message box, the fourth parameter MessageBoxIcon indicates the system icon, and the fifth parameter MessageBoxDefaultButton indicates the button selected by default in the prompt box
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box and provide the "OK" button in the message box dialog box!", "Test test", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box, and provide "OK" and "Cancel" buttons in the message box dialog box!", "Test test", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box, and provide "retry" and "Cancel" buttons in the message dialog box!", "Test test", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box. Provide "yes" and "no" buttons in the message box dialog box!", "Test test", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Test the message dialog box. The message dialog box provides three buttons: Yes, no and cancel!", "Test test", MessageBoxButtons.YesNoCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }
    }
}

summary

In this article, bloggers introduced the use of MessageBox.show's message box. Its six message prompt box buttons show the style effect and its attribute name. For example, OK is an OK button. The message prompt box is frequently used in C# form application programming. Bloggers sometimes use this message prompt box to act as a breakpoint, Although bloggers know how to use breakpoints in vs2019, they still like to use this MessageBox.show to output the values I need. Hahaha, the output type of MessageBox.show is String type. If you use the shaping output, you need to use Convert.ToString(). Well, it's not easy to create, like comments, pay attention to collection!!!

Tags: C# Back-end Embedded system Visual Studio visualstudio

Posted on Tue, 23 Nov 2021 20:55:03 -0500 by skkeeper