473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Update control when property haschanged

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(DesignerSerializat ionVisibility.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
{
...
}
}
Feb 18 '07 #1
1 2076
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.

Feb 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PAUL EDWARDS | last post by:
I have a windows form that is bound to a datatable. In VB6 I could just update the field contents and it would be updated in the database, however if I update the text property of the control from...
1
by: Ahmed Zammar | last post by:
I'm using oledb. how do I update a database after a record has been modified in the data set? thanks.
0
by: Patrick B | last post by:
I'm wondering about the mechanics of databinding. Say that a textbox is bound to a property of a business object. The business object is a "Person". The control is a textbox called...
17
by: Zvi Danovich | last post by:
Hi, I am a newby in ASP.NET, and till now wrote only simple "classic" WEB-sites. But - the time came, and now I need that: 1. Server will "listen" for some events on its local machine 2....
5
by: JohnR | last post by:
I have multiple controls set to the same database and, normally, when I change the record in one control the position is also changed in the other controls because I have "syncwithcurrencymanager'...
1
by: --== Alain ==-- | last post by:
Hi, I have a collection property in my custom control like this one : public ColumnCollection Columns
3
by: pvong | last post by:
VB.NET How do you change a gridview from Update mode to normal mode? I'm usually dealing for Formviews and there is a ChangeMode option but I don't see one for Gridviews.
4
by: John | last post by:
Hello, I was wondering if it was possibly to bind a control to a dataset.haschanges property. The reason I want to do this is so that a little warning shows up on the form saying that the...
5
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.