Playing with winforms textbox border color.

Non classé — Tags :, , , — admin @ 20:12

This is a small hack class that adds a set color for input area borders (textbox, richtextbox, combos, etc.) Here is the result:

Screenshot

Screenshot

It all holds up in a single file:

  1.  
  2.     /// <summary>
  3.     /// This class adds border to input elements similar to Chrome
  4.     /// </summary>
  5.     public class FormBorderizer
  6.     {
  7.         /** PRIVATE MEMBERS **/
  8.         private Label m_lblBorder;                      // label used for borders
  9.         private static Color BORDER_COLOR = Color.Gold; // Border color
  10.         private static int BORDER_WIDTH = 2;            // Border width
  11.         private static Color BACK_COLOR = Color.AliceBlue;       // Back color
  12.  
  13.         /** CONTRUCTORS **/
  14.         /// <summary>
  15.         /// Constructor, adds borders to input areas
  16.         /// </summary>
  17.         /// <param name="Control.ControlCollection">collection of controls to parse</param>
  18.         public FormBorderizer(Control.ControlCollection controlCollection)
  19.         {
  20.             InitializeBorder(controlCollection);
  21.             AddBorder(controlCollection);
  22.         }
  23.  
  24.         /** METHODS **/
  25.  
  26.         /// <summary>
  27.         /// Initializes the border control
  28.         /// </summary>
  29.         /// <param name="controlCollection">collection</param>
  30.         private void InitializeBorder(Control.ControlCollection controlCollection)
  31.         {
  32.             m_lblBorder = new Label();
  33.             m_lblBorder.BackColor = Color.Gold;
  34.             m_lblBorder.Visible = false;
  35.             controlCollection.Add(m_lblBorder);
  36.             m_lblBorder.SendToBack();
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Adds a colored border to a collection of controls
  41.         /// </summary>
  42.         /// <param name="controlCollection">target collection</param>
  43.         private void AddBorder(Control.ControlCollection controlCollection)
  44.         {
  45.             foreach (Control c in controlCollection)
  46.             {
  47.                 // generate event handlers for input areas
  48.                 if (IsInputControl(c))
  49.                 {
  50.                     c.Enter += new EventHandler(c_Enter);
  51.                     c.Leave += new EventHandler(c_Leave);
  52.                 }
  53.                 else if (c is Panel)
  54.                 {
  55.                     new FormBorderizer(c.Controls);
  56.                 }
  57.                 else if (c is TabControl)
  58.                 {
  59.                     TabControl tc = (TabControl)c;
  60.                     foreach (TabPage tp in tc.TabPages)
  61.                     {
  62.                         new FormBorderizer(tp.Controls);
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Checks whether this control should hold a border or not
  70.         /// </summary>
  71.         /// <param name="c">target control</param>
  72.         /// <returns>true or false</returns>
  73.         private bool IsInputControl(Control c)
  74.         {
  75.             return (c is TextBox || c is ListBox || c is ComboBox || c is RichTextBox || c is MaskedTextBox);
  76.         }
  77.  
  78.         /** EVENTS **/
  79.         /// <summary>
  80.         /// Event occuring when the user leaves an input control
  81.         /// </summary>
  82.         private void c_Leave(object sender, EventArgs e)
  83.         {
  84.             m_lblBorder.Visible = false;
  85.             Control ctrl = (Control)sender;
  86.             ctrl.BackColor = Color.White;
  87.            
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Event occuring when the user enters an input control
  92.         /// </summary>
  93.         private void c_Enter(object sender, EventArgs e)
  94.         {
  95.             Control ctrl = (Control)sender;
  96.             WrapControl(ctrl);
  97.             m_lblBorder.Visible = true;
  98.             m_lblBorder.Anchor = ctrl.Anchor;
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Wraps the border around the control
  103.         /// </summary>
  104.         /// <param name="control"></param>
  105.         private void WrapControl(Control control)
  106.         {
  107.             m_lblBorder.Top = control.Top - BORDER_WIDTH;
  108.             m_lblBorder.Left = control.Left - BORDER_WIDTH;
  109.             m_lblBorder.Width = control.Width + 2 * BORDER_WIDTH;
  110.             m_lblBorder.Height = control.Height + 2 * BORDER_WIDTH;
  111.             control.BackColor = BACK_COLOR;
  112.         }
  113.  
  114.     }
  115.  

To call it, simply create an instance of FormBorderizer (such an awful name!!) and initialize it with a form / panel control collection.
Ex:

  1.    public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.             new FormBorderizer(this.Controls);
  7.         }
  8.  
  9.     }
  10.  
  11.  
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2012 Random namespace | powered by WordPress with Barecity