Do you encapsulate (aka lock) values of forms?

Last updated by Igor Goldobin about 2 months ago.See history

One useful feature of inherited forms is the ability to lock the value of certain properties on the inherited copy. E.g.:

  • Font - we want to maintain a consistent font across all forms
  • BackColor - changing the background color prevents the form from being themed
  • Icon - we want all of our forms to have the company Icon

This can be achieved with the following code, which works by hiding the existing property from the designer using the Browsable attribute. The Browsable attribute set to False means "don't show in the the designer". There is also an attribute called EditorBrowsable, which hides the property from intellisense.

C#:

using System.ComponentModel;

[Browsable(false)] // Browsable = show property in the Designer
public new Font Font 
{ 
    get
    {
        return base.Font;
    }
    set
    {
        //base.Font = value; //normal property syntax 
        base.Font = new Font("Tahoma", 8.25); 
        // Must be hard coded - cannot use Me.
    }
}

VB.NET:

Imports System.ComponentModel

<Browsable(False)> _ 
Public Shadows Property Font() As Font
    Get
        Return MyBase.Font 
    End Get
    Set(ByVal Value As Font)
        'MyBase.Font = Value 'normal property syntax
        MyBase.Font = Me.Font 
    End Set
End Property

Figure: Font Property Visible

Figure: Font Property Hidden

We open source. Powered by GitHub