C# 带清除按钮的文本框TextBoxContainClearButton

效果如下:

image

新建一个用户自定义控件,添加1个2个文本框控件,1个按钮,通过属性,设置如下样式:

image

特别要说明的,按钮Text设置为r,Font设置为Marlett,12px。

代码如下:

    public partial class TextBoxContainClearButton : UserControl
    {
        public TextBoxContainClearButton()
        {
            InitializeComponent();
        }

        [Category("自定义"), Description("文本框鼠标悬停提示文本")]
        public string TextBoxToolTipText
        {
            set
            {
                toolTip1.SetToolTip(this.textBoxFront, value);
            }
            get
            {
                return toolTip1.GetToolTip(this.textBoxFront);
            }
        }
        [Category("自定义"), Description("清除按钮鼠标悬停提示文本")]
        public string ClearButtonToolTipText
        {
            set
            {
                toolTip1.SetToolTip(this.buttonClear, value);
            }
            get
            {
                return toolTip1.GetToolTip(this.buttonClear);
            }
        }
        [Category("自定义"), Description("文本框内文本")]
        public string TextBoxText
        {
            set
            {
                textBoxFront.Text = value;
                if (textBoxFront.Text != "")
                {
                    buttonClear.Visible = true;
                }
                else
                {
                    buttonClear.Visible = false;
                }
            }
            get
            {
                return textBoxFront.Text;
            }
        }
        private bool isAutoClear = true;
        [Category("自定义"), Description("是否执行清除")]
        public bool IsAutoClear
        {
            set
            {
                isAutoClear = value;
               
            }
            get
            {
                return isAutoClear;
            }
        }
        private bool textBoxReadOnly = true;
         [Category("自定义"), Description("文本框只读")]
       public bool TextBoxReadOnly
        {
            set
            {
                textBoxReadOnly = value;
                textBoxFront.ReadOnly = textBoxReadOnly;
                textBoxFront.ReadOnly = textBoxReadOnly;
            }
            get
            {
                return textBoxReadOnly;
            }
        }
        [Category("自定义"), Description("文本框背景色")]
        public Color TextBoxBackColor
        {
            set
            {
                textBoxBack.BackColor = value;
                textBoxFront.BackColor = value;
            }
            get
            {
                return textBoxBack.BackColor;
            }
        }
        /// /

        public delegate void ClearButtonClickEventHandler(Object sender, EventArgs e);
        public event ClearButtonClickEventHandler ClearButtonClick; //声明事件
        public class ClearEventArgs : EventArgs
        {
            //将是否取消处理传递给订阅者
            public bool Handled;
            public ClearEventArgs()
            {
                this.Handled = false;
            }
        }
        protected virtual void OnClearButtonClick(EventArgs e)
        {
            if (ClearButtonClick != null)
            { // 如果有对象注册
                ClearButtonClick(this, e);  // 调用所有注册对象的方法
            }
        }


        private void buttonClear_Click(object sender, EventArgs e)
        {
            if (isAutoClear) textBoxFront.Text = "";
            OnClearButtonClick(e);  // 调用 OnButtonClick方法

        }
        /// /

        private void textBoxContainButton_Enter(object sender, EventArgs e)
        {
            textBoxFront.Focus();
        }
        /// /
        public delegate void TextBoxKeyPressEventHandler(Object sender, KeyPressEventArgs e);
        public event TextBoxKeyPressEventHandler TextBoxKeyPress; //声明事件
        protected virtual void OnTextBoxKeyPress(KeyPressEventArgs e)
        {
            if (TextBoxKeyPress != null)
            { // 如果有对象注册
                TextBoxKeyPress(this, e);  // 调用所有注册对象的方法
                
            }
        }

        private void textBoxBefore_KeyPress(object sender, KeyPressEventArgs e)
        {
            OnTextBoxKeyPress(e);  // 调用 OnButtonClick方法

        }
        /// 

        public delegate void TextBoxTextChangedEventHandler(Object sender, EventArgs e);
        public event TextBoxTextChangedEventHandler TextBoxTextChanged; //声明事件
        protected virtual void OnTextBoxTextChanged(EventArgs e)
        {
            if (TextBoxTextChanged != null)
            { // 如果有对象注册
                TextBoxTextChanged(this, e);  // 调用所有注册对象的方法
            }
        }

        private void textBoxBefore_TextChanged(object sender, EventArgs e)
        {
            if (textBoxFront.Text != "")
            {
                buttonClear.Visible = true;
            }
            else
            {
                buttonClear.Visible = false;
            }
            OnTextBoxTextChanged(e);  // 调用 OnButtonClick方法
        }



        /// /




    }

 

最终使用此控件,可用的自定义属性如下:

image

可用的自定义方法如下:

image

转载于:https://www.cnblogs.com/yepoint/p/7258342.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值