Hi

In the VS .Net the Propertygrid has a context menu
with 'Reset' and 'Description' menu points.

I want to have this context menu also for runtime
PropertyGrids.
How can this be done?

Sincerely
orbit

Propertygrid context menu by Iulian

Iulian
Tue Jan 06 15:13:49 CST 2004

You must inherit it and declare your own context menu.
Then catch the click events and show the menu. Then take
the selected property and do a reflection to grab
the "Resetxxxx" method and if found call it. This is how
i did it and it worked fine...

>-----Original Message-----
>Hi
>
>In the VS .Net the Propertygrid has a context menu
>with 'Reset' and 'Description' menu points.
>
>I want to have this context menu also for runtime
>PropertyGrids.
>How can this be done?
>
>Sincerely
>orbit
>.
>

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.


RE: Propertygrid context menu by Jim

Jim
Wed Jan 07 17:31:09 CST 2004

Thank you very much Ying-Shen Yu

Your "code snippet" helped me a lot.

There is only one thing.
Some of my properties do not use the
DefaultPropertyAttribute but the ResetXXXXXX method to
reset. Though I had to enhance your code for these cases.

Which one of these two methods is to be handled first if
(by accident) both are implemented?

I handle the DefaultPropertyAttribute first and invoke the
ResetXXXXXX method only if there is no
DefaultPropertyAttribute. Is this correct?

Sincerely
orbit


>-----Originalnachricht-----
>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.
>
>.
>

RE: Propertygrid context menu by v-yiy

v-yiy
Wed Jan 07 23:44:48 CST 2004

Hi orbit,

I'm glad to hear my snippet is helpful to you.

In your second question, which approach should apply, when both of them
were defined.
In my quick test, the VS.NET IDE uses the DefaultPropertyAttribute when
both exists.
So your current implementation should be fine.
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.


Re: Propertygrid context menu by Andrew

Andrew
Fri Jan 09 21:54:31 CST 2004

Why not just get the PropertyDescriptor from the SelectedGridItem and call
its ShouldSerializeValue method?

""Ying-Shen Yu[MSFT]"" <v-yiy@online.microsoft.com> wrote in message
news:BJJP3pa1DHA.3532@cpmsftngxa07.phx.gbl...
> Hi orbit,
>
> I'm glad to hear my snippet is helpful to you.
>
> In your second question, which approach should apply, when both of them
> were defined.
> In my quick test, the VS.NET IDE uses the DefaultPropertyAttribute when
> both exists.
> So your current implementation should be fine.
> 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.
>



Re: Propertygrid context menu by v-yiy

v-yiy
Sat Jan 10 00:12:19 CST 2004

Hi Andrew,

Yes, the ShouldSerializeValue will make things easier, I digging the
documentation again and found a more suitable method CanResetValue.
Then the code can be simplified to
<code>
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
menuItem1.Enabled =
item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject);
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);
propertyGrid1.Refresh();
}
menuItem1.Enabled =
item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject);
</code>
Thank you for your suggestion, I learn some useful methods from it!

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.