C# 带清除按钮的文本框TextBoxContainClearButton
效果如下:
新建一个用户自定义控件,添加1个2个文本框控件,1个按钮,通过属性,设置如下样式:
特别要说明的,按钮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方法 } /// ///////////////////////////////////////////// }
最终使用此控件,可用的自定义属性如下:
可用的自定义方法如下: