C#中 MessageBox的使用技巧
作者:IC00 时间:2023-06-25 16:15:48
前言
我们在学习Windows应用程序开发中,经常会用到消息对话框给用户或者管理员一些的消息提示,它们都是基于对MessageBox类的消息对话框的一种应用,在C#中,MessageBox消息对话框位于System.Windows.Forms命名空间中。一般情况下,一个消息对话框包含信息提示文字内容,消息对话框标题文字,用户响应按钮及信息图标的内容,我们可以根据自己的需求设置消息对话框。好了我们开始学习吧!!!!
1.创建窗体文件
注:在取文件名或者项目名尽量别和系统的变量名冲突,就是别取一样的比如博主文件名取的MessageBox到后面又要改
1.1 设计界面
2.认识消息对话框的属性和图标
AbortRetryIgnore 在消息框对话框中提供“中止”、“重试”和“忽略”三个按钮
OK 在消息框对话框中提供“确定”按钮
OKCancel 在消息框对话框中提供“确定”和“取消”两个按钮
RetryCancel 在消息框对话框中提供“重试”和“取消”两个按钮
YesNo 在消息框对话框中提供“是”和“否”两个按钮
YesNoCancel 在消息框对话框中提供“是”、“否”和“取消”三个按钮
3.MessageBox消息对话框实现效果
3.1 AbortRetryIgnore效果展示
消息提示框类型使用 AbortRetryIgnore在消息框对话框中提供“中止”、“重试”和“忽略”三个按钮,图标设置为Warning,警告图标
3.2 OK效果展示
消息提示框类型使用OK在消息框对话框中提供“确定”按钮,图标设置为Asterisk,消息图标
3.3 OKCancel效果展示
消息提示框类型使用OKCancel在消息框对话框中提供“确定”和“取消”两个按钮,图标设置为Error,错误警告图标
3.4 RetryCancel效果展示
消息提示框类型使用RetryCancel在消息框对话框中提供“重试”和“取消”两个按钮,图标设置为Question,问号系统图标
3.5 YesNo效果展示
消息提示框类型使用 YesNo在消息框对话框中提供“是”和“否”两个按钮,图标设置为Question,问号系统图标
3.6 YesNoCancel效果展示
消息提示框类型使用YesNoCancel在消息框对话框中提供“是”、“否”和“取消”三个按钮,图标为None空白图标
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);
}
}
}
来源:https://juejin.cn/post/7130242838310158344
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#多线程及同步示例简析
![](https://img.aspxhome.com/file/2023/2/92952_0s.png)
如何在IDEA中对 hashCode()和 equals() 利用快捷键快速进行方法重写
![](https://img.aspxhome.com/file/2023/5/91085_0s.png)
Java二维数组计算集合总结
![](https://img.aspxhome.com/file/2023/8/63998_0s.jpg)
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
Java解析DICOM图之如何获得16进制数据详解
![](https://img.aspxhome.com/file/2023/0/73020_0s.png)
fastjson全局日期序列化设置导致JSONField失效问题解决方案
JVM中的flag设置详解
![](https://img.aspxhome.com/file/2023/7/108207_0s.jpg)
Java 添加、替换、删除PDF中的图片的示例代码
![](https://img.aspxhome.com/file/2023/7/71637_0s.jpg)
Java中遍历Map集合的5种方式总结
Android Studio进行APP图标更改的两种方式总结
![](https://img.aspxhome.com/file/2023/7/111747_0s.png)
C#计算器编写代码
![](https://img.aspxhome.com/file/2023/5/108635_0s.gif)
Andriod 获取电池的信息实例代码
Android 图片处理避免出现oom的方法详解
Eclipse安装Aptana插件(注意对应版本问题)
![](https://img.aspxhome.com/file/2023/5/116125_0s.png)
Android中的LeakCanary的原理详解
![](https://img.aspxhome.com/file/2023/0/100620_0s.png)
SpringBoot拦截器Filter的使用方法详解
![](https://img.aspxhome.com/file/2023/9/79239_0s.png)
c++代码调试方式的几点建议
详解使用Spring Boot开发Restful程序
![](https://img.aspxhome.com/file/2023/3/128863_0s.png)