Yes, you can just add it. The built-in enumeration is called ContentAlignment :
using System.ComponentModel; using System.Windows.Forms; public class newControl : Control { private ContentAlignment _TextAlign = ContentAlignment.MiddleCenter; [Description("The alignment of the text that will be displayed on the control.")] [DefaultValue(typeof(ContentAlignment), "MiddleCenter")] public ContentAlignment TextAlign { get { return _TextAlign; } set { _TextAlign = value; } } }
What you do with this property is up to you now.
Note that I have added some attributes for how the control is used in the PropertyGrid . The DefaultValue attribute does not set the value of the property; it simply determines whether the property is displayed in bold or not.
To display text using the TextAlign property, you will have to override the OnPaint method and draw it:
protected override void OnPaint(PaintEventArgs e) { switch (_TextAlign) { case ContentAlignment.MiddleCenter: { TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); break; } case ContentAlignment.MiddleLeft: { TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, Color.Empty, TextFormatFlags.Left | TextFormatFlags.VerticalCenter); break; }
source share