473,788 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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
{
...
}
}
Feb 18 '07 #1
1 2091
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_GridLine s.ColorChanged -= new
EventHandler(On GridLinesColorC hanged);

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
2439
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 code it is 50% chance that the update will make it back to the dataset. If I update the dataset instead of the form, it does not show on the form. Is there a method that should be used?
1
1744
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
5430
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 "ctlFirstName", and it's bound to the "FirstName" property of "Person". Why doesn't the following work? myForm.Show(); Person p = new Person();
17
2742
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. According to event received (say - event ID), it will update client pages, that need updating based on this ID (if currently there exist clients that are interesting in specific ID). The good example of such site - airport flight information, when...
5
2017
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' set to true for all the controls. I'm assuming that actual syncwithcurrencymanager for all the controls gets executed when the event "BindingManagerPositionChanged" fires. The problem occurs when I set a RowFilter on the defaultview of the...
1
1563
by: --== Alain ==-- | last post by:
Hi, I have a collection property in my custom control like this one : public ColumnCollection Columns
3
7336
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
1312
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 record has not been saved. Thank you heaps for anyones thoughts John Sheppard
5
1793
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 risk of losing some new entries, so I've deceided to create an update thread. This thread is created when the program's start and should (for various reason) run not in sync with the Filemonitor. The Filemonitor event creates an entry in a...
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.