tags:

views:

87

answers:

2

I want all labels inside a detail view to be bold. I created a simple custom label control that forces the font to be bold. This has the feeling of code smell to me. I'm not concerned with a developer being able to customize the custom control (baseDetailLabel). I just want to enforce an application wide standard for detail labels (They must be bold). Is it appropriate to force the font style when the control is initialized or should I be doing this in another method? I do want the style visible from the designer.

public class BaseDetailLabel : System.Windows.Forms.Label
{
    public BaseDetailLabel()
    {
        System.Drawing.Font f = new Font(this.Font,FontStyle.Bold);
        this.Font = f;
    }
}
+2  A: 

From the description I take it that you also have Labels that you don't want to be bold. In that case a separate Control class seems the best way to go.

The simple constructor approach will not prevent override, just sets an initial value. I think that is what you want. To force Bold you would have to override the OnPaint method.

About the smell: it isn't fancy but that comes with the territory. You're enforcing a style rule where there are no good (better) ways of attack. Unless you want to build an entire Style system.

Henk Holterman
+2  A: 

This should be fine. However, I might caution you that you'll probably need to dispose the font somewhere. Fonts are graphical objects which need to be disposed - and I'm not sure if destruction of the label would automatically do that for you.

rein
Updated it with a using statement.
Coov