Uitypeedit
"I want the Red Peach."
If, you're doing a "poker" control, the poker has a property--suit, you want the user to choose the color of this attribute, the property window presents not only the text, there is a small color icon to indicate the color, "red peach" There is a small "red peach" icon in front of the show, "spades" there is a "spade" Icons appear in front, just as you choose the BackColor of other controls, there is a small square color before the color to indicate the selected color, more considerate design ah.
Now, we're going to do this thing:
public class Squeezer
{
.
public CardTypes CardType
{
}
}
[Editor(typeof(CardTypesEditor), typeof(System.Drawing.Design.UITypeEditor))]
public class CardTypes
{
..
}
public class CardTypesEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;//支持画小图
}
public override void PaintValue(PaintValueEventArgs pe) //定义根据值画小图的逻辑
{
string bmpName = null;
CardTypes C = (CardTyes)pe.Value;
switch(C.Value)
{
case CarderTypes.HongTao:
bmpName = "红桃.bmp";//图片必须是嵌入的资源,大小为16*16,类型为BMP
break;
}
Bitmap b = new Bitmap(typeof(GradeEditor), bmpName);
pe.Graphics.DrawImage(b, pe.Bounds);
b.Dispose();
}
}
In the above code, we use the EditorAttribute to make the color class and a editor association, and then through the editor to achieve the function of drawing schematic diagram.
Well, now your creativity may be agitating you to think about a new problem, I don't want to let the user select the value of the attribute simply by rendering a simple drop-down list of values (implemented through Enumconverter), I want to implement the values like BackColor, The dock is such a friendly interaction with the user, well, let's go to the next step.