Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / Windows Forms

An Easy to Use Weak Referenced Event Handler Factory for .NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.84/5 (19 votes)
9 Mar 200710 min read 152.5K   932   87  
An article on creating weak event handlers that show how to use them in a Windows Form.
// ****************************************************************************
// Copyright Swordfish Computing 2005                                        **
//                                                                           **
// Filename: Swordfish\WeakEventHandlerFactoryTester\SubTotal.cs             **
// Authored by: John Stewien of Swordfish Computing                          **
// Date: September 2005                                                      **
//                                                                           **
// - Change Log -                                                            **
// ****************************************************************************
// $Id: SubTotal.cs,v 1.1 2005/09/28 04:55:46 stewienj Exp $

using System;
using System.Collections.Generic;
using System.Text;

namespace Swordfish.WeakEventHandlerFactoryTester
{
	/// <summary>
	/// Event args for when the value in a SubTotal changes
	/// </summary>
	public class SubTotalDiffArgs : EventArgs
	{
		/// <summary>
		/// The amount that the SubTotal object changed by
		/// </summary>
		public readonly float Diff = 0f;
		
		/// <summary>
		/// Constructor. Intializes class fields.
		/// </summary>
		/// <param name="diff"></param>
		public SubTotalDiffArgs(float diff)
		{
			Diff = diff;
		}
	}

	/// <summary>
	/// Class that just holds a float. Fires and event when
	/// the value of the float gets changed.
	/// </summary>
	public class SubTotal
	{
		/// <summary>
		/// The float value
		/// </summary>
		private float subTotal = 0f;

		/// <summary>
		/// Default Constructor.
		/// </summary>
		public SubTotal()
		{
		}

		/// <summary>
		/// Constructor that initializes the float
		/// </summary>
		/// <param name="value"></param>
		public SubTotal(float value)
		{
			subTotal = value;
		}

		/// <summary>
		/// Allow implicit conversion from a float to make it
		/// easier to create an initialized array of SubTotal objects.
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static implicit operator SubTotal(float value)
		{
			return new SubTotal(value);
		}

		/// <summary>
		/// Gets/Sets the value of the float
		/// </summary>
		public float Value
		{
			get
			{
				return subTotal;
			}
			set
			{
				if (subTotal != value)
				{
					float diff = value-subTotal;
					subTotal = value;
					OnSubTotalChanged(diff);
				}
			}
		}

		/// <summary>
		/// Converts this object to a user readable string
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return subTotal.ToString();
		}

		/// <summary>
		/// Event for when the float changes value
		/// </summary>
		public event EventHandler<SubTotalDiffArgs> SubTotalChanged;

		/// <summary>
		/// Fires the SubTotalChanged event
		/// </summary>
		/// <param name="diff"></param>
		private void OnSubTotalChanged(float diff)
		{
			if (SubTotalChanged != null)
			{
				SubTotalChanged(this, new SubTotalDiffArgs(diff));

			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Cheesy Design
Taiwan Taiwan
John graduated from the University of South Australia in 1997 with a Bachelor of Electronic Engineering Degree, and since then he has worked on hardware and software in many fields including Aerospace, Defence, and Medical giving him over 10 of years experience in C++ and C# programming. In 2009 John Started his own contracting company doing business between Taiwan and Australia.

Comments and Discussions