G
Guest
I want to be able to reset a complex property in a PropertyGrid. I know that for properties that are ValueTypes you can include System.ComponentModel.DefaultValue in the declaration of the property. But, for complex property types (e.g., instance types) this does not work because System.ComponentModel.DefaultValue requires a constant value
In order to indicate if a property should be serialized you can include a boolean function named ShouldSerializePropertyName, where PropertyName is the name of your property. Are there similar methods that can be used to allow a property to be reset
Note that I've included some code at the end of this message that illustrates what I want to do
Thanks for any help
Lanc
Public Class Form
Inherits System.Windows.Forms.For
#Region " Windows Form Designer generated code
Public Sub New(
MyBase.New(
'This call is required by the Windows Form Designer
InitializeComponent(
'Add any initialization after the InitializeComponent() cal
Call Me.InitializeAll(
End Su
'Form overrides dispose to clean up the component list
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean
If disposing The
If Not (components Is Nothing) The
components.Dispose(
End I
End I
MyBase.Dispose(disposing
End Su
'Required by the Windows Form Designe
Private components As System.ComponentModel.IContaine
'NOTE: The following procedure is required by the Windows Form Designe
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor
Friend WithEvents Panel1 As System.Windows.Forms.Pane
Friend WithEvents _ResetButton As System.Windows.Forms.Butto
Friend WithEvents _CanResetButton As System.Windows.Forms.Butto
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(
Me.Panel1 = New System.Windows.Forms.Pane
Me._ResetButton = New System.Windows.Forms.Butto
Me._CanResetButton = New System.Windows.Forms.Butto
Me.Panel1.SuspendLayout(
Me.SuspendLayout(
'Panel
Me.Panel1.Controls.Add(Me._ResetButton
Me.Panel1.Controls.Add(Me._CanResetButton
Me.Panel1.Dock = System.Windows.Forms.DockStyle.To
Me.Panel1.Location = New System.Drawing.Point(0, 0
Me.Panel1.Name = "Panel1
Me.Panel1.Size = New System.Drawing.Size(292, 64
Me.Panel1.TabIndex =
'_ResetButto
Me._ResetButton.Location = New System.Drawing.Point(148, 12
Me._ResetButton.Name = "_ResetButton
Me._ResetButton.Size = New System.Drawing.Size(116, 23
Me._ResetButton.TabIndex =
Me._ResetButton.Text = "Reset
'_CanResetButto
Me._CanResetButton.Location = New System.Drawing.Point(12, 12
Me._CanResetButton.Name = "_CanResetButton
Me._CanResetButton.Size = New System.Drawing.Size(116, 23
Me._CanResetButton.TabIndex =
Me._CanResetButton.Text = "Can Reset
'Form
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15
Me.ClientSize = New System.Drawing.Size(292, 258
Me.Controls.Add(Me.Panel1
Me.Name = "Form2
Me.Text = "Form2
Me.Panel1.ResumeLayout(False
Me.ResumeLayout(False
End Su
#End Regio
Protected WithEvents _PropertyGrid As Windows.Forms.PropertyGri
Protected _ClassWithProperties As New Class1.ClassWithPropertie
Public ReadOnly Property PropertyGrid() As Windows.Forms.PropertyGri
Ge
Return Me._PropertyGri
End Ge
End Propert
Private Sub _CanRestoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _CanResetButton.Clic
MessageBox.Show(Me.CanResetSelectedGridItem(False).ToString, "Can Restore", MessageBoxButtons.OK, MessageBoxIcon.Information
End Su
Private Sub _RestoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _ResetButton.Clic
If (Not Me.CanResetSelectedGridItem(True)) The
MessageBox.Show("Unable to reset the selected grid item.", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Error
End If
End Sub
Private Sub InitializeAll()
Me._PropertyGrid = New Windows.Forms.PropertyGrid
Me._PropertyGrid.PropertySort = PropertySort.Alphabetical
Me._PropertyGrid.Dock = DockStyle.Fill
Me.Controls.Add(Me._PropertyGrid)
Me._PropertyGrid.BringToFront()
Me._PropertyGrid.SelectedObject = Me._ClassWithProperties
End Sub
Public Overridable Overloads Function CanResetSelectedGridItem(ByVal resetIfPossible As Boolean) As Boolean
If ((Me._PropertyGrid.SelectedObject Is Nothing) OrElse (Me._PropertyGrid.SelectedGridItem Is Nothing)) Then
Return False
End If
'Determine the component with the property to reset.
Dim componentToReset As Object = Me._PropertyGrid.SelectedObject
'Get Me.SelectedGridItem.PropertyDescriptor.
Dim propertyDescriptor As System.ComponentModel.PropertyDescriptor = Me._PropertyGrid.SelectedGridItem.PropertyDescriptor
'Validate propertyDescriptor.
If (propertyDescriptor Is Nothing) Then
Return False
End If
'Determine if propertyDescriptor can be reset.
Dim canReset As Boolean = propertyDescriptor.CanResetValue(componentToReset) 'Return value.
'Reset propertyDescriptor if required.
If (canReset AndAlso resetIfPossible) Then
propertyDescriptor.ResetValue(componentToReset)
Me._PropertyGrid.Refresh()
End If
Return canReset
End Function
End Class
Public Class Class1
Public Class ClassWithProperties
'NOTE: Value1 and Value2 are initialized to values that do not equal the default values so that the values can be reset.
Protected _Value1 As Integer = 54321
Protected _Value2 As Drawing.Font = New Drawing.Font(Windows.Forms.Control.DefaultFont, FontStyle.Bold)
'Value1
<System.ComponentModel.Description("This property can be reset because it includes System.ComponentModel.DefaultValue."), _
System.ComponentModel.Category("Behavior"), _
System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.DefaultValue(12345), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)> _
Property Value1() As Integer
Get
Return Me._Value1
End Get
Set(ByVal value As Integer)
Me._Value1 = value
End Set
End Property
'Value2
Private Function ShouldSerializeValue2() As Boolean
Return (Not Drawing.Font.Equals(Me._Value2, Windows.Forms.Control.DefaultFont))
End Function
<System.ComponentModel.Description("I want to know how to be able to reset the value of this property as well."), _
System.ComponentModel.Category("Behavior"), _
System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.TypeConverter(GetType(Drawing.FontConverter))> _
Property Value2() As Drawing.Font
Get
Return Me._Value2
End Get
Set(ByVal value As Drawing.Font)
Me._Value2 = value
End Set
End Property
End Class
End Class
In order to indicate if a property should be serialized you can include a boolean function named ShouldSerializePropertyName, where PropertyName is the name of your property. Are there similar methods that can be used to allow a property to be reset
Note that I've included some code at the end of this message that illustrates what I want to do
Thanks for any help
Lanc
Public Class Form
Inherits System.Windows.Forms.For
#Region " Windows Form Designer generated code
Public Sub New(
MyBase.New(
'This call is required by the Windows Form Designer
InitializeComponent(
'Add any initialization after the InitializeComponent() cal
Call Me.InitializeAll(
End Su
'Form overrides dispose to clean up the component list
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean
If disposing The
If Not (components Is Nothing) The
components.Dispose(
End I
End I
MyBase.Dispose(disposing
End Su
'Required by the Windows Form Designe
Private components As System.ComponentModel.IContaine
'NOTE: The following procedure is required by the Windows Form Designe
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor
Friend WithEvents Panel1 As System.Windows.Forms.Pane
Friend WithEvents _ResetButton As System.Windows.Forms.Butto
Friend WithEvents _CanResetButton As System.Windows.Forms.Butto
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent(
Me.Panel1 = New System.Windows.Forms.Pane
Me._ResetButton = New System.Windows.Forms.Butto
Me._CanResetButton = New System.Windows.Forms.Butto
Me.Panel1.SuspendLayout(
Me.SuspendLayout(
'Panel
Me.Panel1.Controls.Add(Me._ResetButton
Me.Panel1.Controls.Add(Me._CanResetButton
Me.Panel1.Dock = System.Windows.Forms.DockStyle.To
Me.Panel1.Location = New System.Drawing.Point(0, 0
Me.Panel1.Name = "Panel1
Me.Panel1.Size = New System.Drawing.Size(292, 64
Me.Panel1.TabIndex =
'_ResetButto
Me._ResetButton.Location = New System.Drawing.Point(148, 12
Me._ResetButton.Name = "_ResetButton
Me._ResetButton.Size = New System.Drawing.Size(116, 23
Me._ResetButton.TabIndex =
Me._ResetButton.Text = "Reset
'_CanResetButto
Me._CanResetButton.Location = New System.Drawing.Point(12, 12
Me._CanResetButton.Name = "_CanResetButton
Me._CanResetButton.Size = New System.Drawing.Size(116, 23
Me._CanResetButton.TabIndex =
Me._CanResetButton.Text = "Can Reset
'Form
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15
Me.ClientSize = New System.Drawing.Size(292, 258
Me.Controls.Add(Me.Panel1
Me.Name = "Form2
Me.Text = "Form2
Me.Panel1.ResumeLayout(False
Me.ResumeLayout(False
End Su
#End Regio
Protected WithEvents _PropertyGrid As Windows.Forms.PropertyGri
Protected _ClassWithProperties As New Class1.ClassWithPropertie
Public ReadOnly Property PropertyGrid() As Windows.Forms.PropertyGri
Ge
Return Me._PropertyGri
End Ge
End Propert
Private Sub _CanRestoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _CanResetButton.Clic
MessageBox.Show(Me.CanResetSelectedGridItem(False).ToString, "Can Restore", MessageBoxButtons.OK, MessageBoxIcon.Information
End Su
Private Sub _RestoreButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _ResetButton.Clic
If (Not Me.CanResetSelectedGridItem(True)) The
MessageBox.Show("Unable to reset the selected grid item.", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Error
End If
End Sub
Private Sub InitializeAll()
Me._PropertyGrid = New Windows.Forms.PropertyGrid
Me._PropertyGrid.PropertySort = PropertySort.Alphabetical
Me._PropertyGrid.Dock = DockStyle.Fill
Me.Controls.Add(Me._PropertyGrid)
Me._PropertyGrid.BringToFront()
Me._PropertyGrid.SelectedObject = Me._ClassWithProperties
End Sub
Public Overridable Overloads Function CanResetSelectedGridItem(ByVal resetIfPossible As Boolean) As Boolean
If ((Me._PropertyGrid.SelectedObject Is Nothing) OrElse (Me._PropertyGrid.SelectedGridItem Is Nothing)) Then
Return False
End If
'Determine the component with the property to reset.
Dim componentToReset As Object = Me._PropertyGrid.SelectedObject
'Get Me.SelectedGridItem.PropertyDescriptor.
Dim propertyDescriptor As System.ComponentModel.PropertyDescriptor = Me._PropertyGrid.SelectedGridItem.PropertyDescriptor
'Validate propertyDescriptor.
If (propertyDescriptor Is Nothing) Then
Return False
End If
'Determine if propertyDescriptor can be reset.
Dim canReset As Boolean = propertyDescriptor.CanResetValue(componentToReset) 'Return value.
'Reset propertyDescriptor if required.
If (canReset AndAlso resetIfPossible) Then
propertyDescriptor.ResetValue(componentToReset)
Me._PropertyGrid.Refresh()
End If
Return canReset
End Function
End Class
Public Class Class1
Public Class ClassWithProperties
'NOTE: Value1 and Value2 are initialized to values that do not equal the default values so that the values can be reset.
Protected _Value1 As Integer = 54321
Protected _Value2 As Drawing.Font = New Drawing.Font(Windows.Forms.Control.DefaultFont, FontStyle.Bold)
'Value1
<System.ComponentModel.Description("This property can be reset because it includes System.ComponentModel.DefaultValue."), _
System.ComponentModel.Category("Behavior"), _
System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.DefaultValue(12345), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible)> _
Property Value1() As Integer
Get
Return Me._Value1
End Get
Set(ByVal value As Integer)
Me._Value1 = value
End Set
End Property
'Value2
Private Function ShouldSerializeValue2() As Boolean
Return (Not Drawing.Font.Equals(Me._Value2, Windows.Forms.Control.DefaultFont))
End Function
<System.ComponentModel.Description("I want to know how to be able to reset the value of this property as well."), _
System.ComponentModel.Category("Behavior"), _
System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Visible), _
System.ComponentModel.TypeConverter(GetType(Drawing.FontConverter))> _
Property Value2() As Drawing.Font
Get
Return Me._Value2
End Get
Set(ByVal value As Drawing.Font)
Me._Value2 = value
End Set
End Property
End Class
End Class