473,231 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

How to reset a complex property in PropertyGrid

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.PropertyDescript or

'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.Com ponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.DefaultValue(12345), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibil ity(System.ComponentModel.DesignerSerializationVis ibility.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.Com ponentModel.RefreshProperties.All), _
System.ComponentModel.Bindable(False), _
System.ComponentModel.Browsable(True), _
System.ComponentModel.DesignerSerializationVisibil ity(System.ComponentModel.DesignerSerializationVis ibility.Visible), _
System.ComponentModel.TypeConverter(GetType(Drawin g.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

Nov 20 '05 #1
2 4811
Lance,

"Lance" <zi***@hotmail.com> schrieb im Newsbeitrag
news:35**********************************@microsof t.com...
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?

use ResetPropertyName (no kidding), that works.

Klaus
Nov 20 '05 #2
That's funny. I love simple answers! Thanks a lot for the help.
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Dunaway | last post by:
I am using the PropertyGrid and I have set the SelectedObject property to an instance of my custom class. One of the properties of the custom class is a bitmap. The PropertyGrid lets me assign...
3
by: Dave Girvitz | last post by:
I have a PropertyGrid (Windows Forms App) based component that uses TypeConverters to generate ranges of acceptable values for properties. The idea was that I could download the key/value pairs...
7
by: steve bull | last post by:
if I have a property something like : public int Width { get {return m_width} set {m_width = value} }
7
by: siddhiash | last post by:
Hi Friends I want to add PasswordChar Property which shows ****** for string which I type in PropertyGrid Control. Regards, Siddharth
5
by: Ger | last post by:
The propertygrid is a great control, but I would like to show a more descriptive text for the properties in the control. I tried to find a solution within the system.componentmodel but did not...
1
by: John Kraft | last post by:
Hi all, I'm trying to use a property grid, and what I'm trying to do seems to really elude me. I've been using the property grid for a short period of time, and this is the first thing I...
0
by: James Arnold | last post by:
Is it possible to reset all the GridItems within a PropertyGrid to be reset, without recursively looping through each item? I know of PropertyGrid.ResetSelectedProperty, but doesn't this mean I'd...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
10
by: Derek Hart | last post by:
I can set focus to my property grid by using propgrid.Focus - but how can I set the default property that is always first? Or just set focus to the default property? Can this be done?
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.