How to create a Bold Label control?

I am trying to create a Shortcut element that automatically displays its text in bold.

My environment is a C # Windows Forms application using .NET 3.5, Visual Studio 2010 SP1, Windows 7 Professional, SP1, a 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 that he uses bold to draw his text.

Below are some problems with the current implementation:

  • I intend to use this bold shortcut control in many places of a large application, resulting in hundreds of instances of this control being executed at runtime. Am I really creating new font objects? Should these font objects be deleted? If so, when?

  • I want to make sure that when I localize my application (set the Localizable property to true for the parent container), this bold label will work well with the WinForm resource serialization mechanism. In other words, if I drop this bold label on the Windows form, then set Localizable to true for the form, and then click Save, Visual Studio will serialize my form resources in MyForm.Designer.cs. This will include an instance of my bold label management. Will my font installation implementation for my bold label ruin this resource serialization mechanism?

  • Is there a more efficient / clean 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; } } } } 
+4
source share
1 answer

I do not understand why this will not 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 to proper serialization is to ensure that the get operation is always cheap, so do your work in set . There should be no problem creating too many Font objects; it will create exactly as much as it takes to complete the work, and the GC will pick up any leftovers (for example, the value from the given operation will have a reference count reduced after completion of the set, and then the GC will process it later).

+5
source

Source: https://habr.com/ru/post/1383485/


All Articles