Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created June 5, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/92a841c07d1fc80e4f0979e40dbb15a0 to your computer and use it in GitHub Desktop.
Save tluyben/92a841c07d1fc80e4f0979e40dbb15a0 to your computer and use it in GitHub Desktop.
Placeholder Winforms
class InternalTextBox : MaskedTextBox
{
bool _PlaceholderHandled = false;
void DoPlaceholder()
{
if (Placeholder.Length > 0 && this.Text == string.Empty && this.Text != this.Placeholder)
{
this.Text = Placeholder;
this.ForeColor = Color.Gray;
}
if (!_PlaceholderHandled)
{
_PlaceholderHandled = true;
this.Leave += (object sender, EventArgs e) => {
DoPlaceholder();
};
this.Click += (object sender, EventArgs e) => {
this.Text = string.Empty;
this.ForeColor = Color.Black;
};
}
}
string _Placeholder = "";
public string Placeholder
{
get
{
return _Placeholder;
}
set
{
this._Placeholder = value;
DoPlaceholder();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment