FaceMessageBox.cs 3.7 KB
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Asa.FaceControl
{
    public partial class FaceMessageBox : FormBase
    {
        private readonly Dictionary<MessageBoxButtons, Action> msgButton;
        private readonly DialogResult[] dialog;
        private readonly FaceButton[] button;

        public FaceMessageBox(string title, string text, MessageBoxButtons button)
        {
            InitializeComponent();
            ctlClose.Click += CtlClose_Click;
            CanResize = false;

            dialog = new DialogResult[3];
            this.button = new FaceButton[3] { BtnOne, BtnTwo, BtnThree };
            BtnOne.Click += BtnMessage_Click;
            BtnTwo.Click += BtnMessage_Click;
            BtnThree.Click += BtnMessage_Click;

            msgButton = new Dictionary<MessageBoxButtons, Action>
            {
                { MessageBoxButtons.OK, MsgOK },
                { MessageBoxButtons.OKCancel, MsgOKCancel },
                { MessageBoxButtons.AbortRetryIgnore, MsgAbortRetryIgnore },
                { MessageBoxButtons.YesNoCancel, MsgYesNoCancel },
                { MessageBoxButtons.YesNo, MsgYesNo },
                { MessageBoxButtons.RetryCancel, MsgRetryCancel }
            };

            Text = title;
            faceLabel1.Text = text;
            msgButton[button].Invoke();
        }

        protected override void CalcSize()
        {
            base.CalcSize();
            int n = 1;
            if (ctlClose != null)
            {
                ctlClose.Size = new Size(60, TitleHeight - 5);
                n += ctlClose.Width;
                ctlClose.Location = new Point(Width - BorderWidth - n, BorderWidth + 1);
                ctlClose.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            }
        }



        private void MsgOK()
        {
            button[2].Text = "OK";
            dialog[2] = DialogResult.OK;
            button[0].Visible = button[1].Visible = false;
        }

        private void MsgOKCancel()
        {
            button[1].Text = "OK";
            dialog[1] = DialogResult.OK;
            button[2].Text = "Cancel";
            dialog[2] = DialogResult.Cancel;
            button[0].Visible = false;
        }

        private void MsgAbortRetryIgnore()
        {
            button[0].Text = "Abort";
            dialog[0] = DialogResult.Abort;
            button[1].Text = "Retry";
            dialog[1] = DialogResult.Retry;
            button[2].Text = "Ignore";
            dialog[2] = DialogResult.Ignore;
        }

        private void MsgYesNoCancel()
        {
            button[0].Text = "Yes";
            dialog[0] = DialogResult.Yes;
            button[1].Text = "No";
            dialog[1] = DialogResult.No;
            button[2].Text = "Cancel";
            dialog[2] = DialogResult.Cancel;
        }

        private void MsgYesNo()
        {
            button[1].Text = "Yes";
            dialog[1] = DialogResult.Yes;
            button[2].Text = "No";
            dialog[2] = DialogResult.No;
            button[0].Visible = false;
        }

        private void MsgRetryCancel()
        {
            button[1].Text = "Retry";
            dialog[1] = DialogResult.Retry;
            button[2].Text = "Cancel";
            dialog[2] = DialogResult.Cancel;
            button[0].Visible = false;
        }




        private void CtlClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnMessage_Click(object sender, EventArgs e)
        {
            FaceButton btn = sender as FaceButton;
            int index = Array.FindIndex(button, s => s == btn);
            if (index == -1) return;
            DialogResult = dialog[index];
        }
    }
}