Skip to content

[API Proposal]: Remove the DefaultValueAttribute from properties that need dynamically calculated default values in WinForms controls #81937

Closed

Description

@Tanya-Solyanik

Background and motivation

Some WinForms control properties are decorated with a DefaultValueAttribute. When control is created in the designer, and the property had not been initialized yet, property browser reads attribute value and shows it in the property browser. If the user changes such a value in the property browser, design time serialization writes the user-specified value into the InitializeComponent method in the code-behind file. If the user does not assign specific value to such a property in the property browser, this property will not be serialized into the code-behind file, as controlled on line 1176 by the type description provider. Default value is also used to reset property value in the property browser.

At the runtime, properties that are not assigned to in the InitializeComponent method, are initialized to default runtime values.

Design time components assume that the runtime default value is the same as the design time one i.e., in the case when the attribute is used, the same as the attribute value. If this assumption is not upheld, the reset option in the property browser in the IDE will not work, and InitializeComponent will explicitly serialize default values into the InitializeComponent method instead of letting the control pick up the runtime default on the initialization.

The motivation

For Height or Width properties, the default runtime value is dependent on the application font. Some of these values had been calculated and hardcoded before we have changed the default font. Currently some of the design time default values defined in this attribute are out of sync with the runtime default values that are explicitly assigned in the constructor.

Example

Design time default value -
https://github.com/dotnet/winforms/blob/62edc1ab7b601bee58199fdea2580a54cf5cd25b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewRow.cs#L212-L218

Runtime default -
https://github.com/dotnet/winforms/blob/62edc1ab7b601bee58199fdea2580a54cf5cd25b/src/System.Windows.Forms/src/System/Windows/Forms/DataGridViewRow.cs#L37

The runtime value is dynamically calculated based on the default font while the designer assumes a constant value.

We received several bug report that follow this pattern:

  1. when designer is shown, control is instantiated, and the runtime default value is assigned to the property.
  2. when control is serialized, TypeDescriptor compares the actual property value (the runtime default) against the value in the attribute, sees that they are different and adds a line that sets this value to the InitalizeComponent.
    Problem: User had not edited this value and expects that height is picked up from the font, user changes the control font on the startup and the height is not scaled for the new font.

API Proposal

Use an alternative mechanism to define the default design time values. This is already used by other properties and is supported in the TypeDescriptor, see line 1170.

  1. remove the default value attribute from properties that have dynamic default values.
  2. add Reset<Property> and ShouldSerialize<Property> methods that compare actual value against the dynamic runtime default.

This is a pseudocode representation of the pattern.

before:

namespace System.Windows.Forms;

public class SomeControl : Control
{
    private const int DefaultHeight = 22;

    public SomeControl()
    {
        Height = DefaultHeight;
    }

    [DefaultValue(DefaultHeight)] 
    public int Height {set; get;}
}

after:

namespace System.Windows.Forms;

public class SomeControl : Control
{
    public SomeControl()
    {
        Height = DefaultHeight;
    }

    private int DefaultFont
    {
        get
        {
            // Calculate height to accommodate text that uses the default font, i.e. use Control.DefaultFont.Height
        }
    }

    public int Height {set; get;}

    private void ResetHeight()
    {
        Height = DefaultHeight;
    }

    private bool ShouldSerializeHeight()
    {
        return Height != DefaultHeight;
    }
}

API Usage

This is a proposal to remove an attribute and replace it with a different mechanism that supports the same design time functionality.

Alternative Designs

No response

Risks

This attribute implements design time functionality that should be accessed through the TypeDescriptor. WinForms team considers this a grey area case of breaking changes because design time functionality is preserved.

Activity

  1. added
    api-suggestion
    on Feb 10, 2023
  2. ghost added
    untriaged
    on Feb 10, 2023
  3. changed the title [-][API Proposal]: Removed the DefaultValueAttribute from properties that need dynamically calculated default values in WinForms controls[/-] [+][API Proposal]: Remove the DefaultValueAttribute from properties that need dynamically calculated default values in WinForms controls[/+] on Feb 10, 2023
  4. added this to the Future milestone on Feb 15, 2023
  5. ghost removed
    untriaged
    on Feb 15, 2023
  6. Tanya-Solyanik commented on Feb 24, 2023

    @Tanya-Solyanik
    Author
  7. ghost locked as resolved and limited conversation to collaborators on Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.ComponentModel

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions