• [C#]关于自己编写MesasgeBox


        public partial class MsgWindow : XtraMessageBoxForm
        {
            #region 属性
            /// <summary>
            /// 换行的长度
            /// </summary>
            public int LineLength = 24;

            private string _MessageString = string.Empty;
            private bool Expanded = false;
            private IconStyle _IconStyleVar = 0;
            private ButtonStyle _ButtonStyleVar = 0;
            private DefaultButton _DefaultButtonVar = 0;
            private DialogResult _DialogResultVar;

            public IconStyle IconStyleVar
            {
                get
                {
                    return _IconStyleVar;
                }
                set
                {
                    if (this._IconStyleVar != value)
                        this._IconStyleVar = value;
                }
            }

            public ButtonStyle ButtonStyleVar
            {
                get
                {
                    return _ButtonStyleVar;
                }
                set
                {
                    if (this._ButtonStyleVar != value)
                        this._ButtonStyleVar = value;
                }
            }

            public DefaultButton DefaultButtonVar
            {
                get
                {
                    return _DefaultButtonVar;
                }
                set
                {
                    if (this._DefaultButtonVar != value)
                        this._DefaultButtonVar = value;
                }
            }

            public DialogResult DialogResultVar
            {
                get
                {
                    return _DialogResultVar;
                }
            }

            #endregion

            #region 构造

            public MsgWindow()
            {
                InitializeComponent();
            }

            private MsgWindow(string Msg)
            {
                InitializeComponent();
                _MessageString = Msg;
                _ButtonStyleVar = ButtonStyle.Ok;
                _IconStyleVar = IconStyle.Information;

                LoadTexts(Msg, string.Empty, string.Empty, string.Empty);
                ControlWindowsSize(MsgWindowSizeCtrl.Up);
                CreateButtons(_ButtonStyleVar);
                LoadImages(_IconStyleVar);
                FocusDefaultButton(DefaultButton.ISNULL);
            }

            private MsgWindow(string Msg, string Details)
            {
                InitializeComponent();
                _MessageString = Msg;
                _ButtonStyleVar = ButtonStyle.Ok;
                _IconStyleVar = IconStyle.Information;

                LoadTexts(Msg, string.Empty, string.Empty, Details);
                ControlWindowsSize(MsgWindowSizeCtrl.Up);
                CreateButtons(_ButtonStyleVar);
                LoadImages(_IconStyleVar);
                FocusDefaultButton(DefaultButton.ISNULL);
            }

            private MsgWindow(string Msg, IconStyle Icon, string Details)
            {
                InitializeComponent();
                _MessageString = Msg;
                _ButtonStyleVar = ButtonStyle.Ok;
                _IconStyleVar = Icon;

                LoadTexts(Msg, string.Empty, string.Empty, Details);
                ControlWindowsSize(MsgWindowSizeCtrl.Up);
                CreateButtons(_ButtonStyleVar);
                LoadImages(_IconStyleVar);
                FocusDefaultButton(DefaultButton.ISNULL);
            }

            private MsgWindow(string Msg, ButtonStyle Button, DefaultButton DefaultButtonVar, string Details)
            {
                InitializeComponent();
                _MessageString = Msg;
                _ButtonStyleVar = Button;
                _IconStyleVar = IconStyle.Information;

                LoadTexts(Msg, string.Empty, string.Empty, Details);
                ControlWindowsSize(MsgWindowSizeCtrl.Up);
                CreateButtons(_ButtonStyleVar);
                LoadImages(_IconStyleVar);
                FocusDefaultButton(DefaultButtonVar);
            }

            private MsgWindow(string Msg, ButtonStyle Button, DefaultButton DefaultButtonVar, IconStyle Icon, string Details)
            {
                InitializeComponent();
                _MessageString = Msg;
                _ButtonStyleVar = Button;
                _IconStyleVar = Icon;

                LoadTexts(Msg, string.Empty, string.Empty, Details);
                ControlWindowsSize(MsgWindowSizeCtrl.Up);
                CreateButtons(_ButtonStyleVar);
                LoadImages(_IconStyleVar);
                FocusDefaultButton(DefaultButtonVar);
            }

            #endregion

            #region 默认枚举

            public enum ButtonStyle
            {
                ISNULL,
                Ok,
                OKCancel,
                OKCancelHelp,
                OKHelp,
                YesNo,
                YesNoCancel,
                YesNoCancelHelp,
                YesNoHelp

            }

            public enum IconStyle
            {
                ISNULL,
                Error,
                Information,
                None,
                Question,
                Stop,
                Warning

            }

            public enum DefaultButton
            {
                ISNULL,
                Cancel,
                Help,
                No,
                Ok,
                Yes
            }

            private enum MsgWindowSizeCtrl
            {
                Up,
                Down
            }

            #endregion

            #region 界面生成

            /// <summary>
            /// 填充界面文本 如果使用默认 请使用string.Empty
            /// </summary>
            /// <param name="Caption"></param>
            /// <param name="Details"></param>
            private void LoadTexts(string MsgStr, string Caption,string GroupCation, string Details)
            {
                this.Text = string.Empty;
                //if (Caption == string.Empty)
                //{
                //    this.Text = "用户提示";
                //}
                //else
                //{
                //    this.Text = Caption;
                //}

                if (GroupCation == string.Empty)
                {
                    groupControl1.Text = "信息";
                }
                else
                {
                    groupControl1.Text = GroupCation;
                }

                if (Details == string.Empty)
                {
                    memoEdit1.Text = "没有关于这个提示的详细信息.";
                }
                else
                {
                    memoEdit1.Text = Details;
                }
            }

            /// <summary>
            /// 控制窗体收起放下
            /// </summary>
            /// <param name="MsgWindowSizeCtrlVar"></param>
            private void ControlWindowsSize(MsgWindowSizeCtrl MsgWindowSizeCtrlVar)
            {
                switch (MsgWindowSizeCtrlVar)
                {
                    case MsgWindowSizeCtrl.Up:
                        this.Size = new Size(416, 170);
                        break;

                    case MsgWindowSizeCtrl.Down:
                        this.Size = new Size(416, 325);
                        break;
                }
               
            }

            /// <summary>
            /// 生成按钮
            /// </summary>
            /// <param name="buttonStyle">按钮风格</param>
            private void CreateButtons(ButtonStyle buttonStyle)
            {
                SimpleButton CBSimBut1 = new SimpleButton();           
                SimpleButton CBSimBut2 = new SimpleButton();
                SimpleButton CBSimBut3 = new SimpleButton();
                SimpleButton CBSimBut4 = new SimpleButton();

                CBSimBut1.Click += new EventHandler(OnButtonClick);
                CBSimBut2.Click += new EventHandler(OnButtonClick);
                CBSimBut3.Click += new EventHandler(OnButtonClick);
                CBSimBut4.Click += new EventHandler(OnButtonClick);
           
                Size size = new Size(75, 22);
                Point but1of1 = new Point(166, 141);
                Point but1of2 = new Point(108, 141);
                Point but2of2 = new Point(221, 141);
                Point but1of3 = new Point(46, 141);
                Point but2of3 = new Point(160, 141);
                Point but3of3 = new Point(274, 141);
                Point but1of4 = new Point(16, 141);
                Point but2of4 = new Point(115, 141);
                Point but3of4 = new Point(214, 141);
                Point but4of4 = new Point(313, 141);
                switch (buttonStyle)
                {
                    case ButtonStyle.ISNULL:

                        break;

                    case ButtonStyle.Ok:
                        CBSimBut1.Name = "OkButton";
                        CBSimBut1.Text = "确定";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of1;
                        this.Controls.Add(CBSimBut1);
                        break;

                    case ButtonStyle.OKCancel:
                        CBSimBut1.Name = "OkButton";
                        CBSimBut1.Text = "确定";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of2;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "CancelButton";
                        CBSimBut2.Text = "取消";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of2;
                        this.Controls.Add(CBSimBut2);
                        break;

                    case ButtonStyle.OKCancelHelp:
                        CBSimBut1.Name = "OkButton";
                        CBSimBut1.Text = "确定";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of3;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "CancelButton";
                        CBSimBut2.Text = "取消";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of3;
                        this.Controls.Add(CBSimBut2);
                        CBSimBut3.Name = "HelpButton";
                        CBSimBut3.Text = "帮助";
                        CBSimBut3.Size = size;
                        CBSimBut3.Location = but3of3;
                        this.Controls.Add(CBSimBut3);
                        break;

                    case ButtonStyle.OKHelp:
                        CBSimBut1.Name = "OkButton";
                        CBSimBut1.Text = "确定";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of2;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "HelpButton";
                        CBSimBut2.Text = "帮助";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of2;
                        this.Controls.Add(CBSimBut2);
                        break;

                    case ButtonStyle.YesNo:
                        CBSimBut1.Name = "YesButton";
                        CBSimBut1.Text = "是";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of2;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "NoButton";
                        CBSimBut2.Text = "否";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of2;
                        this.Controls.Add(CBSimBut2);
                        break;

                    case ButtonStyle.YesNoCancel:
                        CBSimBut1.Name = "YesButton";
                        CBSimBut1.Text = "是";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of3;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "NoButton";
                        CBSimBut2.Text = "否";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of3;
                        this.Controls.Add(CBSimBut2);
                        CBSimBut3.Name = "CancelButton";
                        CBSimBut3.Text = "取消";
                        CBSimBut3.Size = size;
                        CBSimBut3.Location = but3of3;
                        this.Controls.Add(CBSimBut3);
                        break;

                    case ButtonStyle.YesNoCancelHelp:
                        CBSimBut1.Name = "YesButton";
                        CBSimBut1.Text = "是";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of4;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "NoButton";
                        CBSimBut2.Text = "否";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of4;
                        this.Controls.Add(CBSimBut2);
                        CBSimBut3.Name = "CancelButton";
                        CBSimBut3.Text = "取消";
                        CBSimBut3.Size = size;
                        CBSimBut3.Location = but3of4;
                        this.Controls.Add(CBSimBut3);
                        CBSimBut4.Name = "HelpButton";
                        CBSimBut4.Text = "帮助";
                        CBSimBut4.Size = size;
                        CBSimBut4.Location = but4of4;
                        this.Controls.Add(CBSimBut4);
                        break;

                    case ButtonStyle.YesNoHelp:
                        CBSimBut1.Name = "YesButton";
                        CBSimBut1.Text = "是";
                        CBSimBut1.Size = size;
                        CBSimBut1.Location = but1of3;
                        this.Controls.Add(CBSimBut1);
                        CBSimBut2.Name = "NoButton";
                        CBSimBut2.Text = "否";
                        CBSimBut2.Size = size;
                        CBSimBut2.Location = but2of3;
                        this.Controls.Add(CBSimBut2);
                        CBSimBut3.Name = "HelpButton";
                        CBSimBut3.Text = "帮助";
                        CBSimBut3.Size = size;
                        CBSimBut3.Location = but3of3;
                        this.Controls.Add(CBSimBut3);
                        break;
                }
            }

            /// <summary>
            /// 填充图标
            /// </summary>
            /// <param name="Icon"></param>
            private void LoadImages(IconStyle Icon)
            {
                switch (Icon)
                {
                    case IconStyle.Error:
                        //pictureEdit1.Image =
                        break;

                    case IconStyle.Information:
                        groupControl1.Text = "信息";
                        pictureEdit1.Image = global::ESSP.COM.Properties.Resources.Information;
                        break;

                    case IconStyle.ISNULL:

                        break;

                    case IconStyle.None:

                        break;

                    case IconStyle.Question:
                        groupControl1.Text = "询问";
                        pictureEdit1.Image = global::ESSP.COM.Properties.Resources.Information;
                        break;

                    case IconStyle.Stop:

                        break;

                    case IconStyle.Warning:

                        break;
                       
                }
            }

            /// <summary>
            /// 在控件第一次绘制的时候对传入的字符串插入换行
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void labelControl1_Paint(object sender, PaintEventArgs e)
            {
                TextEdit1.Text = _MessageString;
            }

            private void TextEdit1_Paint(object sender, PaintEventArgs e)
            {
                TextEdit1.Text = _MessageString;
            }

            private void FocusDefaultButton(DefaultButton DefaultButtonVar)
            {
                switch (DefaultButtonVar)
                {
                    case DefaultButton.ISNULL:
                        break;
                    case DefaultButton.Ok:
                        foreach (Control con in this.Controls)
                        {
                            if (con.GetType().ToString() != typeof(SimpleButton).ToString())
                            {
                                continue;
                            }
                            if (con.Name == "OkButton")
                            {
                                con.Focus();
                            }
                        }
                        break;

                    case DefaultButton.Cancel:
                        foreach (Control con in this.Controls)
                        {
                            if (con.GetType().ToString() != typeof(SimpleButton).ToString())
                            {
                                continue;
                            }
                            if (con.Name == "CancelButton")
                            {
                                con.Focus();
                            }
                        }
                        break;

                    case DefaultButton.Yes:
                        foreach (Control con in this.Controls)
                        {
                            if (con.GetType().ToString() != typeof(SimpleButton).ToString())
                            {
                                continue;
                            }
                            if (con.Name == "YesButton")
                            {
                                con.Focus();
                            }
                        }
                        break;

                    case DefaultButton.No:
                        foreach (Control con in this.Controls)
                        {
                            if (con.GetType().ToString() != typeof(SimpleButton).ToString())
                            {
                                continue;
                            }
                            if (con.Name == "NoButton")
                            {
                                con.Focus();
                            }
                        }
                        break;

                    case DefaultButton.Help:
                        foreach (Control con in this.Controls)
                        {
                            if (con.GetType().ToString() != typeof(SimpleButton).ToString())
                            {
                                continue;
                            }
                            if (con.Name == "HelpButton")
                            {
                                con.Focus();
                            }
                        }
                        break;

                    default:

                        break;
                }
            }
            #endregion

            #region 按钮处理

            void OnButtonClick(object sender, EventArgs e)
            {
                switch (((SimpleButton)sender).Name)
                {
                    case "OkButton":
                        this.DialogResult = DialogResult.OK;
                        break;

                    case "CancelButton":
                        this.DialogResult = DialogResult.Cancel;
                        break;

                    case "HelpButton":
                        break;

                    case "YesButton":
                        this.DialogResult = DialogResult.Yes;
                        break;

                    case "NoButton":
                        this.DialogResult = DialogResult.No;
                        break;

                    default:
                        this.DialogResult = DialogResult.None;
                        break;
                }
            }

            private void ButtonExpan_Click(object sender, EventArgs e)
            {
                if (Expanded)
                {
                    ControlWindowsSize(MsgWindowSizeCtrl.Up);
                    ButtonExpan.Text = ">>";
                    Expanded = false;
                    return;
                }
                ControlWindowsSize(MsgWindowSizeCtrl.Down);
                ButtonExpan.Text = "<<";
                Expanded = true;
            }

            #endregion

            #region Show方法

            public static DialogResult Show(Exception e)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow("程序的异常",e.Message);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText,DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(Exception e, IconStyle icon)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow("程序的异常", icon, e.Message);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, DefaultButton.ISNULL, string.Empty);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, DefaultButton.ISNULL, DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, IconStyle icon)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, icon, string.Empty);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, IconStyle icon, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, icon, DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, DefaultButton defaultButton)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, defaultButton, string.Empty);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, DefaultButton defaultButton, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, defaultButton, DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, IconStyle icon)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, DefaultButton.ISNULL, icon, string.Empty);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, IconStyle icon, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, DefaultButton.ISNULL, icon, DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, IconStyle icon, DefaultButton defaultButton)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, defaultButton, icon, string.Empty);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            public static DialogResult Show(string messageText, ButtonStyle buttons, IconStyle icon, DefaultButton defaultButton, string DetailText)
            {
                DialogResult DRTmp;
                MsgWindow MsgWin = new MsgWindow(messageText, buttons, defaultButton, icon, DetailText);
                DRTmp = MsgWin.ShowDialog();
                return DRTmp;
            }

            private void MsgWindow_FormClosing(object sender, FormClosingEventArgs e)
            {
                e.Cancel = false;
            }

            #endregion

        }

  • 相关阅读:
    .net core获取数据库连接 抛出The type initializer to throw an exception
    解决本地Bootstrap字体图标不可见的问题
    Bootstrap 遮罩插件jquery.mloading
    oracle 数据库触发器,插入更新时间戳
    入门wpf—— 3、样式
    VisualStudio 连接 MySql 实现增删查改
    Bootstrap-table 使用总结,和其参数说明
    oracle数据库 TIMESTAMP(6)时间戳类型
    3.xadmin配置
    2.Model设计
  • 原文地址:https://www.cnblogs.com/boneking/p/1337761.html
Copyright © 2020-2023  润新知