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
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM