473,480 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4826
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
2067
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
6033
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
7377
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
9699
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
1706
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
6467
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
1205
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
4362
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
9983
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
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5326
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.