// -----------------------------------------------------------------------------
// File name: STextBox.cs
// Author: Smart Goat
// Email: dmtmd2010@yahoo.com.vn
// Blog: knowledgesharez.net
// Date: 2013-12-09
// Description: a text box with placeholder, similar the web input control.
// - Properties:
// + Placeholder: string - The content of the placeholder
// - Methods:
// + IsEmpty(): bool - Check if the text box is empty (recommended)
// -----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FooBar
{
class STextBox : TextBox
{
private bool textInputted;
private Color backupForeColor = Color.Empty;
private string placeholder = string.Empty;
public string Placeholder
{
get { return placeholder; }
set
{
placeholder = value;
SetPlaceHolder();
}
}
public bool IsEmpty()
{
return string.IsNullOrEmpty(Text) || !textInputted;
}
private void SetPlaceHolder()
{
if (string.IsNullOrEmpty(Text))
{
Text = placeholder;
ForeColor = Color.Gray;
textInputted = false;
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
textInputted = !string.IsNullOrEmpty(Text);
}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
if (!textInputted)
{
Text = string.Empty;
ForeColor = backupForeColor;
}
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
SetPlaceHolder();
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
if (backupForeColor == Color.Empty)
{
backupForeColor = ForeColor;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Control && e.KeyCode == Keys.A)
{
SelectAll();
}
}
}
}
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts
Tuesday, December 10, 2013
Textbox with placeholder by C#
It's here. A textbox with placeholder. Look like input control in html. Create a file named STextBox.cs and paste this code:
Subscribe to:
Comments (Atom)