Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / XML

Event Logging in Cx

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
30 Sep 2009CPOL7 min read 34.9K   284   16  
Adding an event logger to Cx.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Cx.Attributes;
using Cx.EventArgs;
using Cx.Interfaces;
using Cx.WinForm;

namespace TextDisplayComponent
{							  
	[CxComponentName("Numeric Display")]
	[CxExplicitEvent("DisplayTextChanged")]
	public partial class TextDisplay : UserControl, ICxVisualComponentClass
	{
		protected enum State
		{
			NewText,
			AddChar,
		}

		protected State state;
		protected bool disposed;

		public TextDisplay()
		{
			InitializeComponent();
			EventHelpers.Transform(this, tbDisplay, "TextChanged", "Text").To("DisplayTextChanged");
			state = State.NewText;
		}

		[CxConsumer]
		public void OnChar(object sender, CxEventArgs<char> args)
		{
			switch (state)
			{
				case State.AddChar:
					tbDisplay.Text = tbDisplay.Text + args.Data;
					break;

				case State.NewText:
					tbDisplay.Text = args.Data.ToString();
					state = State.AddChar;
					break;
			}
		}

		[CxConsumer]
		public void OnText(object sender, CxEventArgs<string> args)
		{
			tbDisplay.Text = args.Data;
			state = State.NewText;
		}

		[CxConsumer]
		public void StateIsNewText(object sender, EventArgs args)
		{
			state = State.NewText;
		}

		public void Register(object form, ICxVisualComponent comp)
		{
			this.RegisterControl((Form)form, comp);
		}

		/// <summary>
		/// We need to remove our event transformations from the helper's dictionary.
		/// </summary>
		/// <param name="disposing"></param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}

			EventHelpers.Remove(this);

			base.Dispose(disposing);
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions