public enum eVisualType
{
None = 0, Torch = 1, Rune01, Rune02, Rune03, FireRed01,
LaserBlackWhiteLeft, LaserBlackWhiteRight, LaserBlueRedLeft, LaserBlueRedRight,
Wheel01, Wheel01a, Wheel02, BlinkingStar, MovingPillar
}
public class EnumTypeConverter : TypeConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true; // True means show a combobox
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true; // True will limit to list. false will show the list, but allow free-formentry
}
}
public class VisualTypeConverter : EnumTypeConverter
{
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new eVisualType[] { eVisualType.BlinkingStar, eVisualType.FireRed01, eVisualType.LaserBlackWhiteLeft, eVisualType.LaserBlackWhiteRight, eVisualType.LaserBlueRedLeft, eVisualType.LaserBlueRedRight, eVisualType.MovingPillar, eVisualType.Rune01, eVisualType.Rune02, eVisualType.Rune03, eVisualType.Torch, eVisualType.Wheel01, eVisualType.Wheel01a, eVisualType.Wheel02 });
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if(value is string)
return (eVisualType)Enum.Parse(typeof(eVisualType), value.ToString(), true);
return base.ConvertFrom(context, culture, value);
}
}
Code in propertygrid class:
private eVisualType m_VisualType = eVisualType.FireRed01;
[CategoryAttribute("Basic"), DescriptionAttribute("The visual type.")]
[TypeConverter(typeof(VisualTypeConverter))]
[DisplayName("Visual Type")]
public eVisualType VisualType
{
get { return m_VisualType; }
set { m_VisualType = value; }
}
The above still produces an error when selecting a different value in the propertygrid at runtime: Object of type 'System.String' cannot be converted to type '[project name].eVisualType'.
The question has been asked before once or twice but never in full detail or it was for WPF+binding only. I use Windows Forms.
I'm also not sure if the ConvertFrom(..) is needed at all.