效果如下:
新建一個用戶自定義控件,添加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方法
}
/// /////////////////////////////////////////////
}
最終使用此控件,可用的自定義屬性如下:
可用的自定義方法如下:



