我有一个名为“PlaceholderTextBox”的类,它可以作为带有占位符文本的文本框:
internal class PlaceholderTextBox : TextBox
{
private bool isPlaceHolder = true;
private bool isPassword;
private string _PlaceholderText;
private string _InputText;
public string PlacehoderText
{
get { return _PlaceholderText; }
set { _PlaceholderText = value; setPlaceholder(); }
}
public string InputText
{
get { return _InputText; }
set { _InputText = value; }
}
public bool IsPassword
{
get { return isPassword; }
set { isPassword = value; }
}
private void setPlaceholder()
{
if (string.IsNullOrEmpty(this.Text))
{
this.Text = PlacehoderText;
InputText = "";
this.ForeColor = System.Drawing.Color.Gray;
isPlaceHolder = true;
if (isPassword) this.UseSystemPasswordChar = false;
}
}
private void removePlaceHolder()
{
if (isPlaceHolder)
{
this.Text = "";
this.ForeColor = System.Drawing.SystemColors.WindowText;
isPlaceHolder = false;
if (isPassword) this.UseSystemPasswordChar = true;
}
}
public void resetText()
{
isPlaceHolder = true;
this.Text = null;
setPlaceholder();
}
public PlaceholderTextBox()
{
Enter += removePlaceHolder;
Leave += setPlaceholder;
TextChanged += addText;
}
private void setPlaceholder(object sender, EventArgs e)
{
setPlaceholder();
}
private void removePlaceHolder(object sender, EventArgs e)
{
removePlaceHolder();
}
private void addText(object sender, EventArgs e)
{
InputText = this.Text;
}
}
在我的主窗体中,我有两个这样的PlaceholderTextBox,一个用于“用户名”,另一个用于“密码”,它还有用于保护输入文本的子弹覆盖。我发现的问题是,如果我想使用“Tab”键从密码文本框切换到另一个,它不起作用。如果它有一些文本或我想从用户名切换到密码,它工作正常,但如果我在密码框中没有任何文本,我想切换到用户名,它不起作用。
我完全不知道为什么不工作,我的猜测是它与密码封面系统有关。关于这个问题的任何见解?