C#公共类 可以谈一个顶部警告框,和一个输入Input用户输入对话框(备忘)

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MessageBoxInfo
{
    public class MessageTopBox : Form
    {
        private MessageTopBox() { }
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MessageTopBox
            // 
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(36, 35);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "MessageTopBox";
            this.ResumeLayout(false);

        }

        /// <summary>
        /// 弹一个顶层对话框
        /// </summary>
        /// <param name="msg">提示警告信息</param>
        /// <param name="caption">提示框表头</param>
        /// <param name="msgBtnType">提示框按钮类型[ok,okCancle...]</param>
        /// <param name="msgBoxIoc">提示文本框图标</param>
        /// <param name="msgSelectBtn">默认选择哪个按钮</param>
        /// <returns>返回用户选择结果[类型;DialogResult]</returns>
        public static DialogResult Show(string msg, string caption, MessageBoxButtons msgBtnType, MessageBoxIcon msgBoxIoc, MessageBoxDefaultButton msgSelectBtn)
        {
            return MessageBox.Show(msg, caption, msgBtnType, msgBoxIoc, msgSelectBtn, MessageBoxOptions.ServiceNotification);
        }

        public static DialogResult Show(string msg, string caption, MessageBoxButtons msgBtnType, MessageBoxIcon msgBoxIoc)
        {
            return Show(msg, caption, msgBtnType, msgBoxIoc, MessageBoxDefaultButton.Button1);
        }
        public static DialogResult Show(string msg, string caption, MessageBoxButtons msgBtnType)
        {
            return Show(msg, caption, msgBtnType, MessageBoxIcon.None);
        }
        public static DialogResult Show(string msg, string caption)
        {
            return Show(msg, caption, MessageBoxButtons.OK);
        }
        public static DialogResult Show(string msg)
        {
            return Show(msg, string.Empty);
        }
        public static DialogResult Show()
        {
            return Show(string.Empty);
        }
    }

    public class InputTopBox : Form
    {
        #region File
        private TextBox txtUserInput;
        private Button btnCancel;
        private Button btnConfirm;
        private Panel panelTop;
        private Panel panelInputArea;

        //用户输入字符串
        internal string UserInputStr = null;

        //标题
        private string CaptionStr = string.Empty;

        internal bool IsAllDisp = false;

        internal bool IsUserSelectOk = false;
        private Panel panelRight;
        private Panel panelLeft;
        private Panel panelCenter;

        internal bool IsFrmCloseing = false;
        #endregion

        private InputTopBox()
        {
            InitializeComponent();
            this.TopLevel = true;
            this.TopMost = true;

        }

        #region ctor

        #endregion


        private void InitializeComponent()
        {
            this.txtUserInput = new System.Windows.Forms.TextBox();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnConfirm = new System.Windows.Forms.Button();
            this.panelTop = new System.Windows.Forms.Panel();
            this.panelInputArea = new System.Windows.Forms.Panel();
            this.panelLeft = new System.Windows.Forms.Panel();
            this.panelRight = new System.Windows.Forms.Panel();
            this.panelCenter = new System.Windows.Forms.Panel();
            this.panelInputArea.SuspendLayout();
            this.panelCenter.SuspendLayout();
            this.SuspendLayout();
            // 
            // txtUserInput
            // 
            this.txtUserInput.Location = new System.Drawing.Point(27, 16);
            this.txtUserInput.Name = "txtUserInput";
            this.txtUserInput.Size = new System.Drawing.Size(313, 21);
            this.txtUserInput.TabIndex = 0;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(235, 68);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(61, 25);
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnConfirm
            // 
            this.btnConfirm.Location = new System.Drawing.Point(332, 68);
            this.btnConfirm.Name = "btnConfirm";
            this.btnConfirm.Size = new System.Drawing.Size(61, 25);
            this.btnConfirm.TabIndex = 2;
            this.btnConfirm.Text = "确认";
            this.btnConfirm.UseVisualStyleBackColor = true;
            this.btnConfirm.Click += new System.EventHandler(this.btnConfirm_Click);
            // 
            // panelTop
            // 
            this.panelTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelTop.Location = new System.Drawing.Point(0, 0);
            this.panelTop.Name = "panelTop";
            this.panelTop.Size = new System.Drawing.Size(405, 10);
            this.panelTop.TabIndex = 3;
            // 
            // panelInputArea
            // 
            this.panelInputArea.Controls.Add(this.panelCenter);
            this.panelInputArea.Controls.Add(this.panelRight);
            this.panelInputArea.Controls.Add(this.panelLeft);
            this.panelInputArea.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelInputArea.Location = new System.Drawing.Point(0, 10);
            this.panelInputArea.Name = "panelInputArea";
            this.panelInputArea.Size = new System.Drawing.Size(405, 49);
            this.panelInputArea.TabIndex = 4;
            // 
            // panelLeft
            // 
            this.panelLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panelLeft.Location = new System.Drawing.Point(0, 0);
            this.panelLeft.Name = "panelLeft";
            this.panelLeft.Size = new System.Drawing.Size(13, 49);
            this.panelLeft.TabIndex = 1;
            // 
            // panelRight
            // 
            this.panelRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panelRight.Location = new System.Drawing.Point(395, 0);
            this.panelRight.Name = "panelRight";
            this.panelRight.Size = new System.Drawing.Size(10, 49);
            this.panelRight.TabIndex = 2;
            // 
            // panelCenter
            // 
            this.panelCenter.Controls.Add(this.txtUserInput);
            this.panelCenter.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panelCenter.Location = new System.Drawing.Point(13, 0);
            this.panelCenter.Name = "panelCenter";
            this.panelCenter.Size = new System.Drawing.Size(382, 49);
            this.panelCenter.TabIndex = 3;
            // 
            // InputTopBox
            // 
            this.ClientSize = new System.Drawing.Size(405, 107);
            this.Controls.Add(this.panelInputArea);
            this.Controls.Add(this.panelTop);
            this.Controls.Add(this.btnConfirm);
            this.Controls.Add(this.btnCancel);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "InputTopBox";
            this.Text = "输入信息:";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.InputTopBox_FormClosing);
            this.Load += new System.EventHandler(this.InputTopBox_Load);
            this.panelInputArea.ResumeLayout(false);
            this.panelCenter.ResumeLayout(false);
            this.panelCenter.PerformLayout();
            this.ResumeLayout(false);

        }

        //窗体加载事件
        private void InputTopBox_Load(object sender, EventArgs e)
        {
            this.btnCancel.Size = this.btnConfirm.Size;
            this.Location = MousePosition;
        }

        #region Static Method

        /// <summary>
        /// 弹出一个用户输入对话框(会阻塞调用线程,直到用户输入或者取消)
        /// </summary>
        /// <param name="title">对话框标题</param>
        /// <param name="IsDefaultOK">是否默认选择ok</param>
        /// <param name="IsMultiline">是否多行显示</param>
        /// <param name="defaultInputStr">默认显示字体</param>
        /// <returns>返回用户输入结果,为null则代表用户取消输入</returns>
        public static string ShowBox(string title, bool IsDefaultOK, bool IsMultiline, string defaultInputStr)
        {
            string UserInfo = null;
            InputTopBox box = null;
            ThreadPool.UnsafeQueueUserWorkItem(_ =>
            {
                box = new InputTopBox();
                if (!string.IsNullOrEmpty(title))
                {
                    box.Text = title;
                }
                if (IsDefaultOK)
                {
                    box.btnConfirm.TabIndex = 1;
                }
                if (IsMultiline)
                {
                    box.txtUserInput.Multiline = true;
                    box.txtUserInput.Dock = DockStyle.Fill;
                }
                if (!string.IsNullOrEmpty(defaultInputStr))
                {
                    box.txtUserInput.Text = defaultInputStr;
                }
                box.ShowDialog();
            }, null);
            while (true)
            {
                Thread.Sleep(1000);
                if (box != null && box.IsFrmCloseing)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(100);
                }
            }

            if (box.IsUserSelectOk)
            {
                UserInfo = box.UserInputStr;
            }
            box.IsAllDisp = true;
            return UserInfo;
        }

        public static string ShowBox(string title)
        {
            return ShowBox(title, null);
        }

        public static string ShowBox(string title, string defaultInputStr)
        {
            return ShowBox(title, false, false, defaultInputStr);
        }
        public static string ShowBox(string title, bool IsDefaultOK, string defaultInputStr)
        {
            return ShowBox(title, IsDefaultOK, false, defaultInputStr);
        }

        public static string ShowBox(bool IsDefaultOK)
        {
            return ShowBox(string.Empty, IsDefaultOK, false, null);
        }

        public static string ShowBox()
        {
            return ShowBox(string.Empty, false, false, null);
        }
        #endregion

        #region WinFrm Designer
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.IsUserSelectOk = true;
            this.UserInputStr = txtUserInput.Text;
            this.Close();
        }


        private void InputTopBox_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.IsFrmCloseing = true;
            while (!IsAllDisp)
            {
                Thread.Sleep(100);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值