/* Made by Lonami Exo
* 24 - Jan. - 2015 -
* (C) LonamiWebs. */
using System.Drawing;
using System.Windows.Forms;
public class TextBoxPlaceholder : TextBox {
public string Placeholder;
public Color PlaceholderColor = Color.Gray;
Color TrueForeColor;
bool HasInit;
public override string Text {
get {
if (HasInit && IsEmpty)
return string.Empty;
return base.Text;
}
set { base.Text = value; }
}
string GetText() { return base.Text; }
public TextBoxPlaceholder() {
TrueForeColor = ForeColor;
GotFocus += TextBoxPlaceholder_GotFocus;
LostFocus += TextBoxPlaceholder_LostFocus;
}
protected override void OnCreateControl()
{
if (Text.Length == 0)
TextBoxPlaceholder_LostFocus(null, null);
base.OnCreateControl();
HasInit = true;
}
void TextBoxPlaceholder_GotFocus(object sender, System.EventArgs e)
{
if (IsPlaceHolder) {
ForeColor = TrueForeColor;
Clear();
}
}
void TextBoxPlaceholder_LostFocus(object sender, System.EventArgs e)
{
if (Text.Length == 0) {
Text = Placeholder;
ForeColor = PlaceholderColor;
}
}
bool IsPlaceHolder {
get {
return ForeColor == PlaceholderColor && GetText() == Placeholder;
}
}
bool IsEmpty {
get { return IsPlaceHolder || GetText().Length == 0; }
}
}
Have you ever wanted a TextBox with a placeholder? No? Well, you should, it's pretty cool, and funny, look!
:D
See? It's funny.
:D
See? It's funny.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.