The blog has moved to a new address. The blog is now located at http://devintelligence.com

Adsense

Monday, February 05, 2007

Propertygrid: How to display combo with icons for enum type?

We need to cretate new type inhereted from UITypeEditor with custom painting( in our case the images located in resource file - where each image name equals to memeber of the enum ) as shown below

 

public class CountryEditor : UITypeEditor
{
///<summary>
///Indicates whether the specified context supports
/// painting a representation of an object's value within the specified context.
///</summary>
///
///<returns>
///true if
/// <see cref="M:System.Drawing.Design.UITypeEditor.PaintValue(System.Object,System.Drawing.Graphics,System.Drawing.Rectangle)">
/// </see> is implemented; otherwise, false.
///</returns>
///
///<param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext">
/// </see> that can be used to gain additional context information. </param>
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}

///<summary>
///Paints a representation of the value of an
/// object using the specified <see cref="T:System.Drawing.Design.PaintValueEventArgs"></see>.
///</summary>
///
///<param name="e">A <see cref="T:System.Drawing.Design.PaintValueEventArgs">
/// </see> that indicates what to paint and where to paint it. </param>
public override void PaintValue(PaintValueEventArgs e)
{
string resourcename = Enum.GetName(typeof (Country), e.Value);

// retrive image from resource file
Bitmap countryImage =
(Bitmap)Resources.ResourceManager.GetObject(resourcename);
Rectangle destRect
= e.Bounds;
countryImage.MakeTransparent();

// paint
e.Graphics.DrawImage(countryImage, destRect);

}
}



 


Create new EnumTypeConverter


public class EnumTypeConverter : EnumConverter
{
private Type _enumType;
public EnumTypeConverter(Type type)
:
base(type)
{
_enumType
= type;
}

public override bool CanConvertTo(ITypeDescriptorContext context,
Type destType)
{
return destType == typeof(string);
}

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value, Type destType)
{
FieldInfo fi
= _enumType.GetField(Enum.GetName(_enumType, value));
DescriptionAttribute dna
=
(DescriptionAttribute)Attribute.GetCustomAttribute(
fi,
typeof(DescriptionAttribute));

if (dna != null)
return dna.Description;
else
return value.ToString();
}

public override bool CanConvertFrom(ITypeDescriptorContext context,
Type srcType)
{
return srcType == typeof(string);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture,
object value)
{
foreach (FieldInfo fi in _enumType.GetFields())
{
DescriptionAttribute dna
=
(DescriptionAttribute)Attribute.GetCustomAttribute(
fi,
typeof(DescriptionAttribute));

if ((dna != null) && ((string)value == dna.Description))
return Enum.Parse(_enumType, fi.Name);
}

return Enum.Parse(_enumType, (string)value);
}


}

 


 


Then use attribute Editor


 



[DisplayName("Country")]
[Description(
"Country")]
[TypeConverter(
typeof(EnumTypeConverter))]
[Editor(
typeof(CountryEditor), typeof(UITypeEditor))]
public Country Country
{
get
{
return _Country;
}
set
{
_Country
= value;
}
}


 


 Now we can see the combo box as editor for our Enum type


 


Source ( russian )

No comments: