How to set property to the default value in a PropertyGrid

G

Guest

When viewing the properties of a control in design mode you can reset a property to its defalut value by right clicking the property and selecting Reset from the menu. But, I would like to reset a property to its default value when a class's property is exposed by using a PropertyGrid control at runtime. Is this possible

Thanks
Lance
 
J

Jeffrey Tan[MSFT]

Hi Lance,

Based on my understanding, you want to display the "reset", "description"
context menu for PropertyGrid at runtime.

Actually, at runtime, there will be no that design-time context menu for
your PropertyGrid. But you may add a context menu of this function
yourself.

For "reset" menu item, you may get information of the selected property
through the PropertyGrid.SelectedGridItem.PropertyDescriptor, then you may
determine the if it can reset, through PropertyDescriptor.CanResetValue()
method. At last, you may implement the reset function through
PropertyGrid.SelectedGridItem.PropertyDescriptor.ResetValue method.

For "description" menu item, you may control the function through
PropertyGrid.HelpVisible property.

I have writen a sample for you like this:

1). First, add a context menu in the designer, like this:
Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.MenuItem1, Me.MenuItem2})
Me.MenuItem1.Index = 0
Me.MenuItem1.Text = "Reset"
Me.MenuItem2.Index = 1
Me.MenuItem2.Text = "Description"

2). Apply the "reset" and "description" logic for your context menu.

Private Sub ContextMenu1_Popup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ContextMenu1.Popup
Dim pd As System.ComponentModel.PropertyDescriptor =
Me.PropertyGrid1.SelectedGridItem.PropertyDescriptor
If pd.CanResetValue(Me.TextBox1) Then
Me.MenuItem1.Enabled = True
Else
Me.MenuItem1.Enabled = False
End If

If Me.PropertyGrid1.HelpVisible = True Then
Me.MenuItem2.Checked = True
Else
Me.MenuItem2.Checked = False
End If
End Sub

Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click

Me.PropertyGrid1.SelectedGridItem.PropertyDescriptor.ResetValue(Me.TextBox1)
End Sub

Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Me.PropertyGrid1.HelpVisible = Not Me.PropertyGrid1.HelpVisible
End Sub

Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi zippy,

Thanks for your feedback.

I am glad it makes for you. :)

If you need further help, please feel free to post.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top