I've been searching for a solution but I can't seem to find one, though I guess this might be simple.
I've built a custom control with a property that is a Class. When I change any of this class properties, my control doesn't automatically refresh (neither in the designer nor programmatically). Is there any way to force Invalidate() method?
I've followed some tips that I found, but none seem to work.
Here's a code sample to demonstrate what I'm experiencing.
This is my custom type.
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TextComponent
{
public TextComponent()
{
Text= string.Empty;
Font = Control.DefaultFont;
ForeColor = Control.DefaultForeColor;
}
[NotifyParentProperty(true)]
public string Text { get; set; }
[NotifyParentProperty(true)]
public Font Font { get; set; }
[NotifyParentProperty(true)]
public Color ForeColor { get; set; }
}
And this is my custom control:
public partial class myControl : Control
{
private TextComponent tc = new TextComponent();
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath gp = new GraphicsPath();
Rectangle rect = new Rectangle(1, 1, DisplayRectangle.Width - 3, DisplayRectangle.Height - 3);
gp.AddRectangle(rect);
pe.Graphics.FillPath(new SolidBrush(Color.LightCoral), gp);
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
pe.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
pe.Graphics.DrawString(tc.Text, tc.Font, new SolidBrush(tc.ForeColor), rect, sf);
}
base.OnPaint(pe);
}
[Description("The Text Component for the Control"), Category("Text"), NotifyParentPropertyAttribute(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextComponent TextComponent { get { return tc; } }
public myControl()
{
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public new string Text { get { return string.Empty; } }
}