Click here to Skip to main content
15,896,346 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;
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.Common;
using Cx.EventArgs;
using Cx.Exceptions;
using Cx.Interfaces;
using Cx.WinForm;

namespace Cx.Designer.Components
{
	[CxComponentName("ListView")]
	[CxExplicitEvent("ItemChecked")]
	public partial class ListView : UserControl, ICxVisualComponentClass
	{
		protected EventHelper checkedEvent;
		protected List<ItemValue> headerText;
		protected List<ItemValue> headerWidths;

		[CxComponentProperty]
		public string Label
		{
			get { return label1.Text; }
			set { label1.Text = value; }
		}

		[CxComponentProperty]
		public string ShowCheckBoxes
		{
			get { return lvListView.CheckBoxes.ToString(); }
			set { lvListView.CheckBoxes = Convert.ToBoolean(value); }
		}

		[CxComponentProperty]
		public List<ItemValue> HeaderText
		{
			get { return headerText; }
			set { headerText = value; }
		}

		[CxComponentProperty]
		public List<ItemValue> HeaderWidths
		{
			get { return headerWidths; }
			set { headerWidths = value; }
		}

		//[CxComponentProperty]
		//public string CtrlAnchor
		//{
		//    get { return lvListView.Anchor.ToString(); }
		//    set
		//    {
		//        Anchor = (AnchorStyles)Enum.Parse(typeof(AnchorStyles), value);
		//    }
		//}

		public ListView()
		{
			InitializeComponent();
			headerText = new List<ItemValue>();
			headerWidths = new List<ItemValue>();
			checkedEvent = EventHelpers.CreateEvent<CxObjectState>(this, "ItemChecked");
			lvListView.ItemChecked += new ItemCheckedEventHandler(OnItemChecked);
		}

		[CxConsumer]
		public void OnData(object sender, CxEventArgs<IEnumerable> args)
		{
			lvListView.Items.Clear();
			InitializeColumns();
			lvListView.Sorting = SortOrder.Ascending;

			foreach (object obj in args.Data)
			{
				ListViewItem lvi = new ListViewItem(obj.ToString());
				lvi.Tag = obj;
				lvListView.Items.Add(lvi);
			}
		}

		[CxConsumer]
		public void OnClearAllCheckState(object sender, System.EventArgs args)
		{
			foreach (ListViewItem lvi in lvListView.Items)
			{
				lvi.Checked = false;
			}
		}

		[CxConsumer]
		public void OnSetCheckState(object sender, CxEventArgs<object> args)
		{
			foreach (ListViewItem lvi in lvListView.Items)
			{
				if (lvi.Tag == args.Data)
				{
					lvi.Checked = true;
					break;
				}
			}
		}

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

		protected void InitializeColumns()
		{
			lvListView.Columns.Clear();
			bool useWidths = false;

			if (headerWidths.Count != 0)
			{
				Verify.IsTrue(headerText.Count == headerWidths.Count, "Header width item count does not match header text item count.");
				useWidths = true;
			}

			for (int i = 0; i < headerText.Count; i++)
			{
				ColumnHeader ch = new ColumnHeader();
				ch.Text = headerText[i].Text;

				if (useWidths)
				{
					ch.Width = Convert.ToInt32(headerWidths[i].Text);
				}

				lvListView.Columns.Add(ch);
			}
		}

		protected void OnItemChecked(object sender, ItemCheckedEventArgs e)
		{
			CxObjectState state = new CxObjectState(e.Item.Tag, e.Item.Checked);
			checkedEvent.Fire(state);
		}
	}
}

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