I have the following class called "PlaceholderTextBox" which to act as a textbox with a placeholder text:
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;
}
}
In my main Form, I have two of these PlaceholderTextBoxes, one for "Username" and the other for "Password", which also has bullets cover for protecting the text entered. The problem I found is that, if I want to switch from the password textbox to the other one using "Tab" key, it doesn't work. It works fine if it has some text or if I want to switch from Username to Password, but if I don't have any text in the Password box and I want to switch to Username, it doesn't work.
I have absolutely no idea why is not working, my guess would be it has something to do with the password cover system. Any insights on this issue?