Use Reset and ShouldSerialize to control a property (Windows Forms .NET)
In this article, you learn how to create the Reset<PropertyName>
and ShouldSerialize<PropertyName>
methods to manage a property for the Properties window in Visual Studio. Reset
and ShouldSerialize
are optional methods that you can provide for a property, if the property doesn't have a simple default value. If the property has a simple default value, you should apply the DefaultValueAttribute and supply the default value to the attribute class constructor instead. Either of these mechanisms enables the following features in the designer:
- The property provides visual indication in the property browser if it has been modified from its default value.
- The user can right-click on the property and choose Reset to restore the property to its default value.
- The designer generates more efficient code.
For more information about properties, see Reset and ShouldSerialize.
Supporting code
This article demonstrates the Reset
and ShouldSerialize
methods by creating a compass rose control. If you're working with your own user control, you can skip this section.
Add the following enumeration to your code:
public enum Directions { None, North, NorthEast, East, SouthEast, South, SouthWest, West, NorthWest, }
Public Enum Directions None North NorthEast East SouthEast South SouthWest West NorthWest End Enum
Add a user control named
CompassRose
.Add a property named
Direction
of typeDirections
to the user control.public Directions Direction { get; set; } = Directions.None;
Public Property Direction As Directions = Directions.None
Reset
The Reset<PropertyName>
method resets the corresponding <PropertyName>
property to its default value.
The following code resets the Direction
property to None
, which is considered the default value for the compass rose control:
private void ResetDirection() =>
Direction = Directions.None;
Private Sub ResetDirection()
Direction = Directions.None
End Sub
ShouldSerialize
The ShouldSerialize<PropertyName>
method returns a boolean value that indicates whether or not the backing property has changed from its default value and should be serialized into the designer's code.
The following code returns true when the Direction
property doesn't equal None
, indicating that a direction has been chosen:
private bool ShouldSerializeDirection() =>
Direction != Directions.None;
Private Function ShouldSerializeDirection() As Boolean
Return Direction <> Directions.None
End Function
Example
The following code shows the Reset
and ShouldSerialize
methods for the Direction
property:
public partial class CompassRose : UserControl
{
public Directions Direction { get; set; } = Directions.None;
public CompassRose() =>
InitializeComponent();
private void ResetDirection() =>
Direction = Directions.None;
private bool ShouldSerializeDirection() =>
Direction != Directions.None;
}
Public Class CompassRose
Public Property Direction As Directions = Directions.None
Private Sub ResetDirection()
Direction = Directions.None
End Sub
Private Function ShouldSerializeDirection() As Boolean
Return Direction <> Directions.None
End Function
End Class
.NET Desktop feedback
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for