简体   繁体   中英

How to design a Bold Label control?

I'm trying to create a Label control that automatically displays its text using a bold font.

My environment is a C# Windows Forms application, using .NET 3.5, Visual Studio 2010 SP1, Windows 7 Professional, SP1, 32-bit processor.

My current implementation is shown below.

The only requirement I have for this bold Label control is that it should behave exactly like the standard System.Windows.Forms.Label control (both programmatically and in the WinForm designer environment), except for the fact that it uses a bold font to draw its text.

Here are some concerns I have with the current implementation:

  1. I intend to use this bold label control in many places in a large application, resulting in hundreds of instances of this control at run-time. Am I needlessly creating new Font objects? Should these Font objects be disposed? If so, when?

  2. I want to make sure that when I localize my application (set Localizable property to true for the parent container), this bold label will behave well with the WinForm resource serialization mechanism. In other words, if I drop this bold label onto a Windows Form, then set Localizable to true for the Form, then click Save, Visual Studio will serialize out my Form's resources to MyForm.Designer.cs. This will include an instance of my bold label control. Will my implementation of setting the Font for my bold label screw up this resource serialization mechanism?

  3. Is there a better/cleaner implementation? Other issues to consider?

[Code Follows]

namespace WindowsFormsApplication1
{
using System.ComponentModel;
using System.Drawing;

/// <summary>
/// Represents a standard Windows label with a bolded font.
/// </summary>
public class BoldLabel : System.Windows.Forms.Label
{
    [Browsable( false )]
    [DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
    public override Font Font
    {
        get
        {
            Font currentFont = base.Font;

            if ( currentFont.Bold )
            {
                // Nothing to do, since the current font is already bold.
                //
                return currentFont;
            }

            if ( currentFont.FontFamily.IsStyleAvailable( FontStyle.Bold ) )
            {
                // The current font supports the bold style, so create
                // a bold version of the current font and return it.
                //
                return new Font( currentFont, FontStyle.Bold );
            }
            else
            {
                // The current font does NOT support the bold style, so
                // just return the current font.
                //
                return currentFont;
            }
        }
        set
        {
            // The WinForm designer should never set this font, but we
            // implement this method for completeness.
            //
            base.Font = value;
        }
    }
}
}

I don't see why this wouldn't work for all your use cases:

public partial class BoldLabel : Label
{
    public BoldLabel()
    {
        InitializeComponent();
        base.Font = new Font(base.Font, FontStyle.Bold);
    }

    public override Font Font
    {
        get
        {
            return base.Font;
        }
        set
        {
            base.Font = new Font(value, FontStyle.Bold);
        }
    }
}

The key in handling proper serialization is to ensure the get operation is always cheap, so do your work in the set . There should be no concerns about creating too many Font objects; it will create exactly as many as it requires to do the work, and GC will pick up any leftovers (for example, the value of the set operation will have the reference count reduced once the set is complete, and then GC will handle it later).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM