Felice Pollano Blog

The Official Fatica Labs Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Cambiare il comportamento di ToString con enum

    E' un po' che uso questa strategia, soprattutto perchè mi piace il componente propertygrid, che permette di editare le proprietà pubbliche di un oggetto.  Se una delle proprietà è un enum, viene graziosamente presentato un combobox per scegliere il valore possibile. Tuttavia le scritte presentate sono i nomi interni dell'enum, che possono essere un po' criptici per l'utente finale, e peggio a volte devono essere tradotti. Visto che non si può derivare una classe da un enum per fare l'override di ToString(), una ( possibile ) soluzione è quella di utilizzare un TypeConverter

    xes: 

        [TypeConverter(typeof(SheetForecastingEnumConverter))]
        public enum SheetForecastingEnum
        {
            None,
            Auto,
            UserSelection
        }

     

    quindi si implementa la classe del converter: 

     

    class SheetForecastingEnumConverter : TypeConverter
        {
            public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
            {
                if (value is string)
                {
                    switch (value)
                    {
                        case "None":
                            return SheetForecastingEnum.None;
                       ........ // altri case per i vari valori
                    }
                }
                return base.ConvertFrom(context, culture, value);
            }
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                if (sourceType == typeof(string))
                    return true;
                return base.CanConvertFrom(context, sourceType);
            }
            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                //show combobox
                return true;
            }
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                //show non editable combobox
                return true;
            }
            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                return new StandardValuesCollection
                    (
                        new SheetForecastingEnum[] { SheetForecastingEnum.Auto, SheetForecastingEnum.None, SheetForecastingEnum.UserSelection }
                    );
            }
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType == typeof(string) && value != null)
                {

                    SheetForecastingEnum ct = (SheetForecastingEnum)value;
                    switch (ct)
                    {
                        case SheetForecastingEnum.None:
                            return "Nessuno";

                        ......
                      
                        default:
                            return "?";

                    }
                }
                else
                    return base.ConvertTo(context, culture, value, destinationType);
            }
        }

    ed il gioco è fatto. 

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5

    Tags:
    Posted by Felice on Tuesday, July 31, 2007 5:58 PM
    Permalink | Comments (0) | Post RSSRSS comment feed

    Related posts

    Add comment


    (Will show your Gravatar icon)  

      Country flag




    Live preview

    Friday, November 21, 2008 7:59 PM

    Gravatar