Playing with winforms textbox border color.
This is a small hack class that adds a set color for input area borders (textbox, richtextbox, combos, etc.) Here is the result:
It all holds up in a single file:
-
-
/// <summary>
-
/// This class adds border to input elements similar to Chrome
-
/// </summary>
-
public class FormBorderizer
-
{
-
/** PRIVATE MEMBERS **/
-
private Label m_lblBorder; // label used for borders
-
private static Color BORDER_COLOR = Color.Gold; // Border color
-
private static int BORDER_WIDTH = 2; // Border width
-
private static Color BACK_COLOR = Color.AliceBlue; // Back color
-
-
/** CONTRUCTORS **/
-
/// <summary>
-
/// Constructor, adds borders to input areas
-
/// </summary>
-
/// <param name="Control.ControlCollection">collection of controls to parse</param>
-
public FormBorderizer(Control.ControlCollection controlCollection)
-
{
-
InitializeBorder(controlCollection);
-
AddBorder(controlCollection);
-
}
-
-
/** METHODS **/
-
-
/// <summary>
-
/// Initializes the border control
-
/// </summary>
-
/// <param name="controlCollection">collection</param>
-
private void InitializeBorder(Control.ControlCollection controlCollection)
-
{
-
m_lblBorder = new Label();
-
m_lblBorder.BackColor = Color.Gold;
-
m_lblBorder.Visible = false;
-
controlCollection.Add(m_lblBorder);
-
m_lblBorder.SendToBack();
-
}
-
-
/// <summary>
-
/// Adds a colored border to a collection of controls
-
/// </summary>
-
/// <param name="controlCollection">target collection</param>
-
private void AddBorder(Control.ControlCollection controlCollection)
-
{
-
foreach (Control c in controlCollection)
-
{
-
// generate event handlers for input areas
-
if (IsInputControl(c))
-
{
-
c.Enter += new EventHandler(c_Enter);
-
c.Leave += new EventHandler(c_Leave);
-
}
-
else if (c is Panel)
-
{
-
new FormBorderizer(c.Controls);
-
}
-
else if (c is TabControl)
-
{
-
TabControl tc = (TabControl)c;
-
foreach (TabPage tp in tc.TabPages)
-
{
-
new FormBorderizer(tp.Controls);
-
}
-
}
-
}
-
}
-
-
/// <summary>
-
/// Checks whether this control should hold a border or not
-
/// </summary>
-
/// <param name="c">target control</param>
-
/// <returns>true or false</returns>
-
private bool IsInputControl(Control c)
-
{
-
return (c is TextBox || c is ListBox || c is ComboBox || c is RichTextBox || c is MaskedTextBox);
-
}
-
-
/** EVENTS **/
-
/// <summary>
-
/// Event occuring when the user leaves an input control
-
/// </summary>
-
private void c_Leave(object sender, EventArgs e)
-
{
-
m_lblBorder.Visible = false;
-
Control ctrl = (Control)sender;
-
ctrl.BackColor = Color.White;
-
-
}
-
-
/// <summary>
-
/// Event occuring when the user enters an input control
-
/// </summary>
-
private void c_Enter(object sender, EventArgs e)
-
{
-
Control ctrl = (Control)sender;
-
WrapControl(ctrl);
-
m_lblBorder.Visible = true;
-
m_lblBorder.Anchor = ctrl.Anchor;
-
}
-
-
/// <summary>
-
/// Wraps the border around the control
-
/// </summary>
-
/// <param name="control"></param>
-
private void WrapControl(Control control)
-
{
-
m_lblBorder.Top = control.Top - BORDER_WIDTH;
-
m_lblBorder.Left = control.Left - BORDER_WIDTH;
-
m_lblBorder.Width = control.Width + 2 * BORDER_WIDTH;
-
m_lblBorder.Height = control.Height + 2 * BORDER_WIDTH;
-
control.BackColor = BACK_COLOR;
-
}
-
-
}
-
To call it, simply create an instance of FormBorderizer (such an awful name!!) and initialize it with a form / panel control collection.
Ex:
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
new FormBorderizer(this.Controls);
-
}
-
-
}
-
-
