C# MessageBox使用技巧

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看活动详情

前言:

我们在学习Windows应用程序开发中,经常会用到消息对话框给用户或者管理员一些的消息提示,它们都是基于对MessageBox类的消息对话框的一种应用,在C#中,MessageBox消息对话框位于System.Windows.Forms命名空间中。一般情况下,一个消息对话框包含信息提示文字内容,消息对话框标题文字,用户响应按钮及信息图标的内容,我们可以根据自己的需求设置消息对话框。好了我们开始学习吧!!!!

每日一遍,开心一整天

image-20211121194351477

1.创建窗体文件

注:在取文件名或者项目名尽量别和系统的变量名冲突,就是别取一样的比如博主文件名取的MessageBox到后面又要改

image-20211121110310081

1.1 设计界面

image-20211121114651341

2.认识消息对话框的属性和图标

  AbortRetryIgnore	  在消息框对话框中提供“中止”、“重试”和“忽略”三个按钮
  OK	  在消息框对话框中提供“确定”按钮
  OKCancel	  在消息框对话框中提供“确定”和“取消”两个按钮
  RetryCancel	  在消息框对话框中提供“重试”和“取消”两个按钮
  YesNo	  在消息框对话框中提供“是”和“否”两个按钮
  YesNoCancel	  在消息框对话框中提供“是”、“否”和“取消”三个按钮
复制代码

image-20211121121004582

3.MessageBox消息对话框实现效果

3.1 AbortRetryIgnore效果展示

消息提示框类型使用 AbortRetryIgnore在消息框对话框中提供“中止”、“重试”和“忽略”三个按钮,图标设置为Warning,警告图标

image-20211121115137334

3.2 OK效果展示

消息提示框类型使用OK在消息框对话框中提供“确定”按钮,图标设置为Asterisk,消息图标

image-20211121115433170

3.3 OKCancel效果展示

消息提示框类型使用OKCancel在消息框对话框中提供“确定”和“取消”两个按钮,图标设置为Error,错误警告图标

image-20211121115656449

3.4 RetryCancel效果展示

消息提示框类型使用RetryCancel在消息框对话框中提供“重试”和“取消”两个按钮,图标设置为Question,问号系统图标

image-20211121115943339

3.5 YesNo效果展示

消息提示框类型使用 YesNo在消息框对话框中提供“是”和“否”两个按钮,图标设置为Question,问号系统图标

image-20211121120234524

3.6 YesNoCancel效果展示

消息提示框类型使用YesNoCancel在消息框对话框中提供“是”、“否”和“取消”三个按钮,图标为None空白图标

image-20211121120448432

4.代码展示

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("测试一下消息对话框在消息框对话框中提供“中止”、“重试”和“忽略”三个按钮!", "测试测试", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);//第一个参数文本表示提示内容,第二个参数文本表示消息框标题,第三个参数MessageBoxButtons消息框的按钮样式,第四个参数MessageBoxIcon表示系统图标,第五个参数MessageBoxDefaultButton表示提示框默认选择的按钮
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下消息对话框在消息框对话框中提供“确定”按钮!", "测试测试", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下消息对话框在消息框对话框中提供“确定”和“取消”两个按钮!", "测试测试", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下消息对话框在消息框对话框中提供“重试”和“取消”两个按钮!", "测试测试", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下消息对话框在消息框对话框中提供“是”和“否”两个按钮!", "测试测试", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("测试一下消息对话框在消息框对话框中提供“是”、“否”和“取消”三个按钮!", "测试测试", MessageBoxButtons.YesNoCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }
    }
}

复制代码

总结

博主这篇文章介绍了MessageBox.show的消息框的使用,它6个消息提示框按钮展示样式效果和它的属性名还是很容易理解 的比如OK就是一个确定按钮,消息提示框在C#窗体应用编程中使用频率特别高,博主有时候就经常把这个消息提示框用来起到断点的作用,博主虽然知道vs2019怎么用断点,但是还是喜欢用这个MessageBox.show来输出我需要的值,哈哈哈,MessageBox.show的输出类型是String类型,如果你把整形输出就要用Convert.ToString()转一下。好了创作不易点赞评论关注收藏!!!

5201121

猜你喜欢

转载自juejin.im/post/7130242838310158344