Hi,
I finally found how to update the render of my custom control when one
of its property value changed.
However, i would like to know if there is not another way, because i'm
afraid about memory usage or leak.
The following code is only a change color property update, but it is
easy to adapt it to other properties.
However, i'm scared about the custom control constructor. Every time i
must add/link there an eventhandler... is it a must or does it exist
another solution ?
thanks a lot,
Al.
---------------------
Here is my custom control class code :
namespace MyComponent
{
public partial class Frame : UserControl
{
#region Event Handler
private EventHandler HandlerColorCha nged;
#endregion
public Frame()
{
InitializeCompo nent();
HandlerColorCha nged = new EventHandler(On GridLinePropert yChanged);
this.m_GridLine s.ColorChanged += HandlerColorCha nged;
}
private CGridLine m_GridLines= new CGridLine();
[Category("Appea rance")]
[Browsable(true)]
[DesignerSeriali zationVisibilit y(DesignerSeria lizationVisibil ity.Content)]
[Description("Se tup Style, Type and Color of gridlines to draw.")]
[TypeConverter(t ypeof(CGridLine Converter))]
public CGridLine GridLines
{
get
{
return this.m_GridLine s;
}
set
{
this.m_GridLine s = value;
if (HandlerColorCh anged != null)
{
this.m_GridLine s.ColorChanged -= this.HandlerCol orChanged;
}
this.HandlerCol orChanged = new
EventHandler(On GridLinePropert yChanged);
this.m_GridLine s.ColorChanged += this.HandlerCol orChanged;
}
}
protected override void OnPaint(PaintEv entArgs e)
{
if (this.m_GridLin es.Lines != GridLine.None)
{
Pen myPen = new Pen(this.m_Grid Lines.Color);
e.Graphics.Draw Rectangle(myPen , new
Rectangle(0,0,t his.Width-1,this.Height-1));
}
}
private void OnGridLinePrope rtyChanged(obje ct Sender, EventArgs e)
{
// redraw the control
Invalidate();
}
}
}
and my GridClass code :
namespace MyComponent
{
/// <summary>
/// Specifies how a Table draws grid lines between its rows and columns
/// </summary>
public enum GridLine
{
/// <summary>
/// No grid lines are drawn
/// </summary>
None = 0,
/// <summary>
/// Grid lines are only drawn between columns
/// </summary>
Columns = 1,
/// <summary>
/// Grid lines are only drawn between rows
/// </summary>
Rows = 2,
/// <summary>
/// Grid lines are drawn between rows and columns
/// </summary>
All = 3
}
/// <summary>
/// Specifies the style of the lines drawn when a Table draws its
grid lines
/// </summary>
public enum GridLineStyle
{
/// <summary>
/// Specifies a solid line
/// </summary>
Solid = 0,
/// <summary>
/// Specifies a line consisting of dashes
/// </summary>
Dash = 1,
/// <summary>
/// Specifies a line consisting of dots
/// </summary>
Dot = 2,
/// <summary>
/// Specifies a line consisting of a repeating pattern of dash-dot
/// </summary>
DashDot = 3,
/// <summary>
/// Specifies a line consisting of a repeating pattern of dash-dot-dot
/// </summary>
DashDotDot = 4
}
public class CGridLine
{
#region Event Handler
public event EventHandler ColorChanged;
#endregion
#region Class Data
private GridLine m_GridLines;
private GridLineStyle m_GridLinesStyl e;
private Color m_GridLinesColo r;
#endregion
#region Constructor
public CGridLine()
{
this.m_GridLine s = GridLine.None;
this.m_GridLine sColor = System.Drawing. SystemColors.Ac tiveBorder;
this.m_GridLine sStyle = GridLineStyle.S olid;
}
public CGridLine(GridL ine LineType, GridLineStyle LineStyle, Color
LineColor)
{
this.m_GridLine s = LineType;
this.m_GridLine sColor = LineColor;
this.m_GridLine sStyle = LineStyle;
}
#endregion
#region Properties
/// <summary>
/// Style of the GridLines
/// </summary>
///
[RefreshProperti es(RefreshPrope rties.Repaint)]
[NotifyParentPro perty(true)]
[DefaultValue(ty peof(GridLineSt yle), "Solid")]
public GridLineStyle Style
{
get
{
return this.m_GridLine sStyle;
}
set
{
this.m_GridLine sStyle = value;
//this.OnColorCha nged(EventArgs. Empty);
}
}
/// <summary>
/// Types of GridLines
/// </summary>
[RefreshProperti es(RefreshPrope rties.Repaint)]
[NotifyParentPro perty(true)]
[DefaultValue(ty peof(GridLine), "None")]
public GridLine Lines
{
get
{
return this.m_GridLine s;
}
set
{
this.m_GridLine s = value;
//this.OnProperty Changed(EventAr gs.Empty);
}
}
/// <summary>
/// Color of the GridLine
/// </summary>
[RefreshProperti es(RefreshPrope rties.Repaint)]
[NotifyParentPro perty(true)]
[DefaultValue(ty peof(Color), "ActiveBord er")]
public Color Color
{
get
{
return this.m_GridLine sColor;
}
set
{
if (this.m_GridLin esColor != value)
{
this.m_GridLine sColor = value;
OnColorChanged( EventArgs.Empty );
}
}
}
#endregion
#region Event
private void OnColorChanged( EventArgs e)
{
// event has been raised
if (ColorChanged != null)
{
ColorChanged(th is, e);
}
}
#endregion
}
public class CGridLineConver ter : ExpandableObjec tConverter
{
...
}
}