Update control when property haschanged

A

--== Alain ==--

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 HandlerColorChanged;

#endregion

public Frame()
{
InitializeComponent();
HandlerColorChanged = new EventHandler(OnGridLinePropertyChanged);
this.m_GridLines.ColorChanged += HandlerColorChanged;
}

private CGridLine m_GridLines= new CGridLine();

[Category("Appearance")]
[Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("Setup Style, Type and Color of gridlines to draw.")]
[TypeConverter(typeof(CGridLineConverter))]
public CGridLine GridLines
{
get
{
return this.m_GridLines;
}
set
{
this.m_GridLines = value;
if (HandlerColorChanged != null)
{
this.m_GridLines.ColorChanged -= this.HandlerColorChanged;
}
this.HandlerColorChanged = new
EventHandler(OnGridLinePropertyChanged);
this.m_GridLines.ColorChanged += this.HandlerColorChanged;
}
}

protected override void OnPaint(PaintEventArgs e)
{
if (this.m_GridLines.Lines != GridLine.None)
{
Pen myPen = new Pen(this.m_GridLines.Color);
e.Graphics.DrawRectangle(myPen, new
Rectangle(0,0,this.Width-1,this.Height-1));
}
}

private void OnGridLinePropertyChanged(object 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_GridLinesStyle;
private Color m_GridLinesColor;

#endregion

#region Constructor

public CGridLine()
{
this.m_GridLines = GridLine.None;
this.m_GridLinesColor = System.Drawing.SystemColors.ActiveBorder;
this.m_GridLinesStyle = GridLineStyle.Solid;
}

public CGridLine(GridLine LineType, GridLineStyle LineStyle, Color
LineColor)
{
this.m_GridLines = LineType;
this.m_GridLinesColor = LineColor;
this.m_GridLinesStyle = LineStyle;
}
#endregion

#region Properties

/// <summary>
/// Style of the GridLines
/// </summary>
///
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(GridLineStyle), "Solid")]
public GridLineStyle Style
{
get
{
return this.m_GridLinesStyle;
}
set
{
this.m_GridLinesStyle = value;
//this.OnColorChanged(EventArgs.Empty);
}
}

/// <summary>
/// Types of GridLines
/// </summary>
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(GridLine), "None")]
public GridLine Lines
{
get
{
return this.m_GridLines;
}
set
{
this.m_GridLines = value;
//this.OnPropertyChanged(EventArgs.Empty);
}
}

/// <summary>
/// Color of the GridLine
/// </summary>
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(Color), "ActiveBorder")]
public Color Color
{
get
{
return this.m_GridLinesColor;
}
set
{
if (this.m_GridLinesColor != value)
{
this.m_GridLinesColor = value;
OnColorChanged(EventArgs.Empty);
}
}
}

#endregion

#region Event

private void OnColorChanged(EventArgs e)
{
// event has been raised
if (ColorChanged != null)
{
ColorChanged(this, e);
}
}

#endregion

}

public class CGridLineConverter : ExpandableObjectConverter
{
...
}
}
 
B

Bruce Wood

Don't forget that in your user control you should unsubscribe from the
ColorChanged event in its Dispose() method, so that when it is being
thrown away it removes the reference from the CGridLine object to
itself.

Furthermore, there's no need to store the exactly EventHandler that
you used to subscribe to the ColorChanged event. You can do this:

this.m_GridLines.ColorChanged -= new
EventHandler(OnGridLinesColorChanged);

and it will also work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top