Click here to Skip to main content
15,894,646 members
Articles / Multimedia / GDI+

Customizable Tree Control with Animation Support

Rate me:
Please Sign up or sign in to vote.
4.72/5 (17 votes)
8 Apr 2008CPOL4 min read 108.1K   23   115  
A tree control implementation, allowing complete customization and animation support
/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace BinaryComponents.WinFormsUtility.Controls
{
	public class ListView : System.Windows.Forms.ListView
	{
		public IDisposable SuspendUpdate()
		{
			return new Updater( this );
		}

		public bool CanUpdate
		{
			get
			{
				return _updateDisabledCount == 0;
			}
		}

		public int ExpandingColumn
		{
			get
			{
				return _expandingColumn;
			}
			set
			{
				_expandingColumn = value;
			}
		}

		protected override void OnSizeChanged( EventArgs e )
		{
			base.OnSizeChanged( e );

			if( _expandingColumn >= 0 && _expandingColumn < Columns.Count )
			{
				int width = ClientRectangle.Width - 10;

				foreach( ColumnHeader column in Columns )
				{
					if( column.Index != _expandingColumn )
					{
						width -= column.Width;
					}
				}

				Columns[_expandingColumn].Width = width;
			}
		}

		protected virtual void OnUpdateEnabled()
		{
		}

		#region Updater

		private sealed class Updater : IDisposable
		{
			internal Updater( ListView listView )
			{
				_listView = listView;

				if( _listView._updateDisabledCount == 0 )
				{
					_listView.BeginUpdate();
				}

				_listView._updateDisabledCount++;
			}

			#region IDisposable Members

			public void Dispose()
			{
				if( _listView != null )
				{
					_listView._updateDisabledCount--;

					if( _listView._updateDisabledCount == 0 )
					{
						_listView.OnUpdateEnabled();
						_listView.Invalidate();
						_listView.EndUpdate();
					}
					_listView = null;
				}
			}

			#endregion

			private ListView _listView;
		}

		#endregion

		private int _updateDisabledCount = 0;
		private int _expandingColumn = -1;
	}
}

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
Web Developer
United Kingdom United Kingdom
I'm currently working for a small start-up company, BinaryComponents Ltd, producing the FeedGhost RSS reader.

FeedGhost RSS Reader:
http://www.feedghost.com

Bespoke Software Development
http://www.binarycomponents.com

Comments and Discussions