Propertygrid and exceptions


K

Keith

Looking through the newgroups and google, I can't find any good information
regarding catching exceptions in the PropertyGrid component. If the user
sets an invalid value for a property in my object and my object throws an
exception, the PropertyGrid component displays the error in its own dialog.
I would like to trap the error and display a localized message.

Keith
 
Ad

Advertisements

Y

Ying-Shen Yu[MSFT]

Hi Keith,

Thanks for your post!

From your description, my understanding now is,
You wrote a component and it will throw exception when receiving an invalid
value. You would like to localize this message, so you want to catch this
exception in propergrid component and localize this message on your own.

Based on my research, You may try the following two ways to customize this
message:
1. From the object side, you may derive from your component and write a
wrapper for your component, catch all the exceptions and change them to the
localized message and rethrow them. It might be cleaner solution, since you
only need write a class and all codes are centralized.

2. From the PropertyGrid side, there is no direct way to "Intercept" such
exception, however you may define a typeconverter and added to your
properties, then override the CanConvertFrom method like below;
<code>
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
try
{
//give a new culture to get
return base.ConvertFrom (context, culture, value);
}
catch (UserException ex)
{
//get localized version of ex.Message
throw new Exception("Localized message",ex);
}
}
</code>

Also, if you just want to localize the exception message of .NET defined
exceptions, you may install the language pack and set the
hread.CurrentThread.CurrentUICulture to that locale before base.ConvertFrom
like:
<code>
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN",true);
</code>
We will localize all the other text to your culture.

Does my suggestions solve your problem?
Please let me know if you have any further questions on this issue, 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.
 
K

Keith

Ying-Shen,

Thanks for the detailed response.

Since I had written the object side, I created an event that the owner of
the property grid can connect to. Then, when an exception was going to be
thrown, I check the event and, if not NULL, I send the exception args to the
event. If it is NULL, I throw the exception.

I guess I'm disappointed that there is no way to redirect the exceptions.
With everything in .NET geared towards overriding functionality, this is one
thing that is missing. It really makes the propertygrid unusable in any
application. I mean, who wants the default dialog?

Thanks again,
Keith
 
Ad

Advertisements

Y

Ying-Shen Yu[MSFT]

Hi Keith,

Currently, the PropertyGrid do not have this option. However you may submit
a product feedback to our product group, they will investigate your
feedback and take in consider to improve the future products, it's your
feedback that let us know where should we improve. You may submit the
feedback at

http://register.microsoft.com/mswish/suggestion.asp

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.
 

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

Similar Threads

Exception when editing collection in PropertyGrid 0
PropertyGrid 0
PropertyGrid 0
PropertyGrid 1
PropertyGrid 4
PropertyGrid 1
PropertyGrid 2
PropertyGrid 6

Top