Created
June 5, 2017 11:01
-
-
Save tluyben/92a841c07d1fc80e4f0979e40dbb15a0 to your computer and use it in GitHub Desktop.
Placeholder Winforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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