Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

PropertyGrid

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J-L - 13 Mar 2008 18:01 GMT
Hi,

When I use the propertygrid to display boolean value property, le
choice is always "true or false", in english.

Is it possible to modify it to strings for used culture ?
For exemple, "vrai ou faux" in french ...

Thanks,

J-L
Marc Gravell - 13 Mar 2008 18:53 GMT
Like below? Note that really you should take the culture from the arg
to ConvertTo / ConvertFrom - but I was in a hurry ;-p

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;

class MyDisplayNameAttribute : DisplayNameAttribute
{
   public MyDisplayNameAttribute(string displayName) :
base(displayName) { }
   public override string DisplayName {
       get { // TODO: localize the display name
           return "Cultured " + base.DisplayName;
       }
   }
}
class MyBooleanConverter : BooleanConverter
{
   private readonly string trueVal, falseVal;
   public MyBooleanConverter()
   {
       trueVal = "vrai"; falseVal = "faux";
   }
   public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
   {
       return new StandardValuesCollection(new string[] { trueVal,
falseVal });
   }
   public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
   {
       string sVal = value as string;
       if (sVal != null) {
           if (sVal == trueVal) return true;
           if (sVal == falseVal) return false;
       }
       return base.ConvertFrom(context, culture, value);
   }
   public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
   {
       if (destinationType == typeof(string) && value != null &&
value is bool)
       {
           return ((bool)value) ? trueVal : falseVal;
       }
       return base.ConvertTo(context, culture, value,
destinationType);
   }
}
class Foo
{
   [MyDisplayName("Some Prop")]
   [TypeConverter(typeof(MyBooleanConverter))]
   public bool Bar { get; set; }
}

static class Program
{
   [STAThread]
   static void Main()
   {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Form form = new Form();
       PropertyGrid grid = new PropertyGrid();
       grid.SelectedObject = new Foo();
       grid.Dock = DockStyle.Fill;
       form.Controls.Add(grid);
       Application.Run(form);
   }
}
Marc Gravell - 13 Mar 2008 18:55 GMT
I forgot to say... if you get the values from a resx that will save
you having to do your own culture lookup; note you can also localize
DescriptionAttribute etc just as easily
Marc Gravell - 13 Mar 2008 19:04 GMT
Oops - one minor bug; you don't need to override GetStandardValues,
since this is meant to return the *typed* values (i.e. true/false, not
the strings). If you simply remove this override, then the double-
click behavior starts working correctly ;-p

Any other glitches, let me know...

Marc
J-L - 14 Mar 2008 10:02 GMT
Marc Gravell a pensé très fort :
> Oops - one minor bug; you don't need to override GetStandardValues,
> since this is meant to return the *typed* values (i.e. true/false, not
[quoted text clipped - 4 lines]
>
> Marc

Thanks a lot, it work very well ...

Just 2 questions :

       public MyBooleanConverter()
       {
           trueVal = "vrai";
           falseVal = "faux";
       }

Is it in this place that I need to localize trueval and falseval in
function of culture ?

and in :

       public override string DisplayName
       {
           get
           {
               // TODO: localize the display name
               return "Cultured " + base.DisplayName;
           }
       }

Is it for the CategoryAttribute ?
I have tried to modify base.DisplayName but it is read-only.
Marc Gravell - 14 Mar 2008 21:49 GMT
> Is it in this place that I need to localize trueval and falseval in
> function of culture ?
Actually, no. The "right" place depends on the app. In the more
complex case, if you need to support multiple cultures *at the same
time* then the correct approach would be to use the culture passed
into ConvertTo/ConvertFrom. However, if you only need to support a
single culture (typical for a windows client), then I would use some
static fields:
(this example uses resx file)

   // public ctor (required to work)
   public MyBooleanConverter() { }

   // private static true/false values
   private static readonly string trueVal, falseVal;
   static MyBooleanConverter()
   {
       trueVal =
WindowsFormsApplication1.Properties.Resources.TrueText;
       falseVal =
WindowsFormsApplication1.Properties.Resources.FalseText;
   }

> Is it for the CategoryAttribute ?
The one I posted (DisplayName) controls the name that appears for the
property. Category is done slightly differently:
(this example just shows simple case - but you can generalise using
Properties.Resources.ResourceManager.GetString(whatever))

class MyCategoryAttribute : CategoryAttribute
{
   public MyCategoryAttribute(string category) : base(category) { }

   protected override string GetLocalizedString(string value)
   {
       return "Translated";
   }
}

If you return null from GetLocalizedString() then it uses the original
text as a default. If you return an empty string ("") it dispays
"Misc".
J-L - 17 Mar 2008 11:42 GMT
thanks a lot more ...

Yes, ma soft need to change of culture if the user want it.
An option for that is in a settings panel , and the user restart the
application to get new language in interface.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.