RE: Propertygrid context menu by v-yiy
v-yiy
Tue Jan 06 20:27:48 CST 2004
Hi Jim,
Thanks for your post!
You may create a context menu and make these two menu items by yourself.
the Description is using PropertyGrid.HelpVisible property, the Reset
command needs a bit more work,
1. get the selected property in the propertygrid from
PropertyGrid.SelectedGridItem.
2. get the property descriptor of the selected property descriptor from
GridItem.PropertyDescriptor.
3. get the DefaultValueAttribute of the PropertyDescriptor. if the property
doesn't have one, set the Reset menuitem to disabled.
4. if the DefaultValueAttribute exists, get the current value of this
property and compare it with the default value, set the correct menu state.
5. to reset the value of this property, you may use
PropertyDescriptor.ResetValue method.
Here is a sample snippet, Hope it will be helpful :
<code>
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
DefaultValueAttribute defValue =
item.PropertyDescriptor.Attributes[typeof(DefaultValueAttribute)] as
DefaultValueAttribute;
if (defValue != null && defValue.Value != null)
{
object obj =
item.PropertyDescriptor.GetValue(propertyGrid1.SelectedObject);
menuItem1.Enabled = !(obj.Equals(defValue.Value));
}
else menuItem1.Enabled = false;
menuItem2.Checked = propertyGrid1.HelpVisible;
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
propertyGrid1.HelpVisible = ! propertyGrid1.HelpVisible;
menuItem2.Checked = propertyGrid1.HelpVisible;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
item.PropertyDescriptor.ResetValue(propertyGrid1.SelectedObject);
}
</code>
Does it solve your problem?
If you still have problem on it please be free to reply this thread.
Thanks!
Best regards,
Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.