c# 自定义控件-提示框(弹框)

分带取消按钮和不带取消按钮的

调用方法:

frmMessageBox frm = new frmMessageBox("提示", "数据连接失败,请重试!", 0);
frm.ShowDialog();
frm.Dispose();
GC.Collect();
frmMessageBox frm = new frmMessageBox("删除用户", "确定删除用户?", 1);
frm.ShowDialog();
frm.Dispose();//模式窗体不会自动调用资源清理,需手动清理,否则会内存溢出 
GC.Collect();
if (frm.DialogResult != DialogResult.OK) return;
//执行的操作

自定义:

public partial class frmMessageBox : Form
    {
        private bool normalmoving = false;
        private Point oldMousePosition;

        /// <summary>
        /// type=0/1:0表示无取消按钮;1表示有确定和取消按钮
        /// </summary>
        /// <param name="type"></param>
        public frmMessageBox(int type)
        {
            InitializeComponent();
            InitButton(type);
        }

        private void InitButton(int type)
        {

            if (type == 0)
            {
                this.btnOK.Location = new System.Drawing.Point(195, 160);
                this.button2.Visible = false;
            }
        }

        public frmMessageBox(string Title, string Message, int type):this(type)
        {
            this.Message = Message;
            this.Title = Title;
        }
        /// <summary>
        /// Message
        /// </summary>
        public string Message
        {
            set
            {
                this.lblMessage.Text = value;
            }
        }
        /// <summary>
        /// Title
        /// </summary>
        public string Title
        {
            set
            {
               this.lblTitle.Text = value;

            }
        }


        /// <summary>
        /// 确定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            //1.返回一个值,给调用者
            this.DialogResult = DialogResult.OK;
            //2.关闭
            this.Close();
        }
        /// <summary>
        /// 取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void frmMessageBox_Load(object sender, EventArgs e)
        {

        }
        #region 标题栏的相关事件
        private void pnlTitle_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                return;
            }
            //Titlepanel.Cursor = Cursors.NoMove2D;
            oldMousePosition = e.Location;
            //moving = true;
            normalmoving = true;
        }

        private void pnlTitle_MouseMove(object sender, MouseEventArgs e)
        {
            //如果leftlag为true则进行移动
            if (!normalmoving) return;
            if (e.Button == MouseButtons.Left && normalmoving)
            {
                Point newposition = new Point(e.Location.X - oldMousePosition.X, e.Location.Y - oldMousePosition.Y);
                this.Location += new Size(newposition);
                //if (MousePosition.Y <= 0)
                //{
                //    this.WindowState = FormWindowState.Maximized;
                //    maxmoving = false;
                //}
            }
        }

        private void pnlTitle_MouseUp(object sender, MouseEventArgs e)
        {
            if (normalmoving)
            {
                normalmoving = false;
            }
        }
        #endregion
    }

 

posted @ 2019-12-06 13:29  孤单玻璃杯  阅读(4854)  评论(0编辑  收藏  举报