Do you encapsulate (aka lock) values of forms?

Last updated by Brady Stroud [SSW] about 1 month 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.


using System.ComponentModel;

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


Imports System.ComponentModel

<Browsable(False)> _
Public Shadows Property Font() As Font
        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

Adam Cogan
We open source. Powered by GitHub