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.  

Winforms flicker-free forms.

C/C++, Non classé — Tags :, , — admin @ 0:12

Ever had those flickers in your 50+ component forms?

Trying to play with SuspendLayout / ResumeLayout didn’t help? That’s because it only suspends the automatic layout, triggered by the Anchor and Dock properties.

Setting double-buffering to true didn’t help either? That’s because it only suppresses flicker on individual controls: a label, a button and so on…

Setting the OptimizedDoubleBuffer flag to true? Nope, no changes…

After struggling for many hours trying to eliminate a flicker that was occuring on my application form, I finally found a solution on msdn:

It’s called compositing double-buffering and it’s simply 7 lines of code put in somewhere in your form code:

  1.  
  2. protected override CreateParams CreateParams {
  3.   get {
  4.     CreateParams cp = base.CreateParams;
  5.     cp.ExStyle |= 0×02000000;
  6.     return cp;
  7.   }
  8. }

Explanation from nobugz:

I discovered a new Windows style in the SDK header files, available for Windows XP and (presumably) Vista: WS_EX_COMPOSITED. With that style turned on for your form, Windows XP does double-buffering on the form and all its child controls

nobugz is the man…

Adding programs to path.

Non classé — Tags : — admin @ 15:11

By default, some programs will be installed while automatically being setup in order to be run from the command line.
For ex., [Win]+R ‘devenv’ launches Visual Studio.

I happened to run into this article which precises how to add some more:

Registry: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths

  1. Create a new sub-key with the name of the executable file that you wish to add to the path (ex. eclipse.exe)
  2. In this new key, add a string variable named “Path” containing the value of the the path to your new executable file (ex. “C:\Program files\eclipse\”)
  3. The new key will already have an empty variable (Default). Edit it to have the string value of entire address of the new program executable (ex. “C:\Program files\eclipse\eclipse.exe”)

Masters without style.

Non classé — Tags : — admin @ 18:05

Everytime I start digging into a new technology, I find myself quickly pointed towards some of the most talented computer science geek’s home page. And when I say talented, I mean “genius”… Those are the founding fathers of CS; those are the Ones who shift right four times the price tags of their supermarket nachos to know how much their 3 friends will owe them… In their head of course… Doing so, I eventually find myself hitting their own personal page, clicking that mouse link with as much enthusiasm as a little boy opening up his first nintendo…

And everytime, it happens… I find myself mixed with feelings of admiration and deception. Something like : “this guy is a genius… but he sure has an ugly personal webpage…”

Why is that??

Those guys are pioneers, they have proved to be inovative and creative… When one would expect to see some webpage using advanced techniques, stunning css, unique algorithms, or a perfect mix of simplicity, beauty and speed; we usually end up seeing repulsive design. Instead of seeing an echo of their genius, we see our screen filled up with 100% width / height text, using a max of 3 tags (”a”, “li” & “p”), and webpages that smell like old history books, the web’s first steps, my first 2400bps modem, …

Apart from the nostalgia it creates, I can understand how such a page can have some advantages : “who cares about heavy pages, buggy javascripts that slow down everything, bling-bling UIs that compensates for crappy contents? Let’s make a simple page, it’s fast, easy to maintain, it works everywhere the same, it’s stable, etc…” But still people, there is a balance in all things, even in web design… A little padding here, a little line-height there, some fonts, colors, divs, a bit more space and TADA! Your page becomes so much easier to read, much more attractive and friendlier. All of those little details that keep the speed and the functionalities while bringing a bit of warmth to a rich-yet-cold webpage.

Or has it become a stamp for quality? An ugly, old website now stands for quality content… ? It seems that’s the case for a couple of amazing books, including SICP… :

Or is it a question of humility? pride? I display myself as somewhat simple while having done extraordinary things?

Anyways… I’m not here to judge, I don’t have a personal web page! just this simple wordpress generated blog… I just found it somewhat funny. See it for yourself :

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2010 Random namespace | powered by WordPress with Barecity