I have encountered instances when I needed a high level look and feel of Windows Forms and controls. Of course, I can set property value in each form at design time. But this one-time change is inflexible and impractical when I have to manage hundreds of Window Forms. For example, in multi-lingual capable application, Windows From font is likely to change accordingly to user environment. And the application may not choose the same font as the Windows default setting. To remedy this issue, this obviously requires programmatically changes.
The key to manage this requirement is to enforce look of feel at runtime. My solution is to use helper class and codify look and feel. All Window Forms will make one simple method call to standardize look and feel. This framework not only alleviates developers from mundane change at design time, it also prevents accidental modification without notice.
Here're the steps I take:
- Define your look and feel class.
I create a static class called as UniformLookAndFeel. I codify my look and feel in static method Format.
///
/// This static class enforces look and feel on all Windows Forms.
///
public class UniformLookAndFeel
{
static UniformLookAndFeel() {}
///
/// Define default ui look and feel setting.
///
/// Windows From to be formatted
public static void Format(System.Windows.Forms.Form form)
{
System.Windows.Forms.Control.ControlCollection ctrlCol = form.Controls;
System.Collections.IEnumerator ienum = ctrlCol.GetEnumerator();
ienum.Reset();
while(ienum.MoveNext())
{
Object obj = ienum.Current;
string typeStr = obj.GetType().ToString();
// Define labels look and feel
if(typeStr=="System.Windows.Forms.Label")
{
// Define font
Label label = (Label)obj;
label.Font = new Font("Arial", 9, System.Drawing.FontStyle.Bold);
// Set warning label text color as red
if(label.Name=="lblWarning")
label.ForeColor = System.Drawing.Color.Red;
}
// Define textboxes look and feel
if(typeStr=="System.Windows.Forms.TextBox")
{
// Define font
TextBox textBox = (TextBox)obj;
textBox.Font = new Font("Times New Roman", 9);
}
}
}
}
This method iterates the input form and defines properties on appropriate control. In my case, I want all labels to use bolded Arial font with size 9; for warning label, the font color should be red; for text control, use Times New Roman with size 9.


Raju GattuPosted May 18, 2010, 1:05 AM
This artical very useful for my requirement. But it needs some more information on this. When the win Form contains Panel and Groupbox controls, the UniformLookAndFeel class properties not working. so can you please provide the solution for this. Thanks!