一、.Net基础【1.5】封装MessageBox

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 MessageBoxTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("你好","提示");
            //MessageBox.Show
            /*
            MessageBox.Show("真的要删除吗?", "询问", MessageBoxButtons.AbortRetryIgnore,
                MessageBoxIcon.Error);
             */

            /*
            DialogResult ret1 = MessageBox.Show("真的要删除吗?", "询问", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ret1 == System.Windows.Forms.DialogResult.Yes)
            {
                MessageBox.Show("您选择了删除");
            }
            else
            {
                MessageBox.Show("取消了");
            }
            */
            /*
            MessageBox.Show("删除失败","错误",
                MessageBoxButtons.OK,MessageBoxIcon.Error);
             */
            //ShowError("删除失败");
            if (ShowYesNo("真的要删除吗?"))
            {
                MessageBox.Show("开始删除");
            }
            else
            {
                MessageBox.Show("取消删除");
            }
        }

        /// <summary>
        /// 显示错误消息窗口
        /// </summary>
        /// <param name="msg">要显示的错误消息内容</param>
        static void ShowError(string msg)
        {
            MessageBox.Show(msg, "错误",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 显示一个有【是】【否】按钮的对话框
        /// </summary>
        /// <param name="msg">要显示的消息内容</param>
        /// <returns>true为点击了【是】,false为点击了【否】</returns>
        static bool ShowYesNo(string msg)
        {
            DialogResult ret = MessageBox.Show(msg, "询问", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            /*
            if (ret == DialogResult.Yes)
            {
                return true;
            }
            else
            {
                return false;
            }*/
            return ret == DialogResult.Yes;
        }
    }
}

 

posted @ 2017-12-19 22:46  LolitaGIS的笔记  阅读(527)  评论(1编辑  收藏  举报