public class TextBox : System.Windows.Forms.TextBox
{
//转载时请自觉加上以下转载信息:
/*王伟晔编写代码段
*/
public TextBox()
: base()
{
processDefaultValue();
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
processDefaultValue();
}
protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseClick(e);
base.SelectAll();
}
private System.String strEmptyValue = System.String.Empty;
public System.String EmptyValue//记录空值的表现
{
get { return strEmptyValue; }
set
{
if (base.Text == strEmptyValue)
base.Text = value.Trim();
strEmptyValue = value.Trim();
processDefaultValue(); }
}
protected override void OnValidating(System.ComponentModel.CancelEventArgs e)//覆盖TextBox验证处理
{
processDefaultValue();
base.OnValidating(e);
}
public override string Text//覆盖Text属性
{
get
{
return base.Text.Trim() == strEmptyValue.Trim() ? System.String.Empty : base.Text.Trim();
}
set
{
base.Text = value;
processDefaultValue();
}
}
void processDefaultValue()//处理表现空值的方法
{
base.Text = base.Text.Trim().Length <= 0 ? strEmptyValue : base.Text.Trim();
}
}