1、Winfrom项目经常会使用到消息提示,一般都使用MessageBox.Show方法,但是像错误提示,询问提示,警告提示写起来就有点复杂了,并且后面几种提示都带有图标,但是MessageBox.Show没有图标,会影响项目的一致性。所以封装了一个常用的消息提示类MessageCommon
public static class MessageCommon { /// <summary> /// 根据类型弹出提示框 /// </summary> /// <param name="type">类型 war:警告 que:询问 err:错误 其他默认为消息提示 </param> /// <param name="msg">消息</param> public static DialogResult ShowMassage(string type,string msg) { switch (type) { case "war": return MessageBox.Show(msg, "警告!", MessageBoxButtons.OK, MessageBoxIcon.Warning); case "que": return MessageBox.Show(msg, "是否继续?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); case "err": return MessageBox.Show(msg, "错误!", MessageBoxButtons.OK, MessageBoxIcon.Error); default: return MessageBox.Show(msg, "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } } /// <summary> /// 警告提示 /// </summary> /// <param name="msg">消息内容</param> /// <returns></returns> public static DialogResult ShowWar(string msg) { return ShowMassage("war", msg); } /// <summary> /// 询问提示 /// </summary> /// <param name="msg">消息内容</param> /// <returns>DialogResult.OK or DialogResult.Cancel</returns> public static DialogResult ShowQue(string msg) { return ShowMassage("que", msg); } /// <summary> /// 错误提示 /// </summary> /// <param name="msg">消息内容</param> /// <returns></returns> public static DialogResult ShowErr(string msg) { return ShowMassage("err", msg); } /// <summary> /// 一般提示 /// </summary> /// <param name="msg">消息内容</param> /// <returns></returns> public static DialogResult ShowInf(string msg) { return ShowMassage("inf", msg); } }
2、使用方法如下:
//一般提示 private void button1_Click(object sender, EventArgs e) { MessageCommon.ShowInf("提示!"); } //错误提示 private void button2_Click(object sender, EventArgs e) { MessageCommon.ShowErr("操作有误!"); } //警告提示 private void button3_Click(object sender, EventArgs e) { MessageCommon.ShowWar("禁止操作!"); } //询问提示 private void button4_Click(object sender, EventArgs e) { if (MessageCommon.ShowQue("操作有风险是否继续?")==DialogResult.OK) { MessageCommon.ShowInf("确定"); } else { MessageCommon.ShowInf("取消"); } }
3、运行效果图:
4、示例代码:
http://download.csdn.net/detail/kehaigang29/8832703
时间: 2024-11-07 10:05:36