Click here to Skip to main content
15,886,030 members
Articles / Web Development / ASP.NET

Zeta Enterprise Library

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
16 Jan 2010CPOL3 min read 50.2K   2.3K   48  
A small set of general-purpose classes for using in .NET applications (2.0 or higher)
namespace Zeta.EnterpriseLibrary.Windows.Controls
{
	#region Using directives.
	// ----------------------------------------------------------------------

	using System.ComponentModel;
	using System.Diagnostics;
	using System.Windows.Forms;
	using System.Drawing;

	// ----------------------------------------------------------------------
	#endregion

	/////////////////////////////////////////////////////////////////////////

	/// <summary>
	/// Extended combo box that draws images and indention.
	/// </summary>
	public partial class ImageComboBox :
		ExtendedComboBox
	{
		#region Public methods.
		// ------------------------------------------------------------------

		/// <summary>
		/// Initializes a new instance of the <see cref="ImageComboBox"/> class.
		/// </summary>
		public ImageComboBox()
		{
			InitializeComponent();

			//SetStyle( ControlStyles.AllPaintingInWmPaint, true );
			//SetStyle( ControlStyles.UserPaint, true );
			//_isVista = ((Environment.OSVersion.Version >= new Version( @"6.0.0" )) &&
			//    (Environment.OSVersion.Platform == PlatformID.Win32NT));

			//DropDownStyle = ComboBoxStyle.DropDownList;
			//FlatStyle = FlatStyle.System;
			DrawMode = DrawMode.OwnerDrawFixed;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ImageComboBox"/> class.
		/// </summary>
		/// <param name="container">The container.</param>
		public ImageComboBox(
			IContainer container )
		{
			container.Add( this );

			InitializeComponent();

			//SetStyle( ControlStyles.AllPaintingInWmPaint, true );
			//SetStyle( ControlStyles.UserPaint, true );
			//_isVista =
			//    ((Environment.OSVersion.Version >= new Version( @"6.0.0" )) &&
			//        (Environment.OSVersion.Platform == PlatformID.Win32NT));

			//DropDownStyle = ComboBoxStyle.DropDownList;
			//FlatStyle = FlatStyle.System;
			DrawMode = DrawMode.OwnerDrawFixed;
		}

		// ------------------------------------------------------------------
		#endregion

		#region Public properties,
		// ------------------------------------------------------------------

		/// <summary>
		/// ImageList property.
		/// </summary>
		/// <value>The image list.</value>
		public ImageList ImageList
		{
			get
			{
				return _imageList;
			}
			set
			{
				_imageList = value;
			}
		}

		/// <summary>
		/// Strongly-typed.
		/// </summary>
		/// <value></value>
		/// <returns>The object that is the currently selected item or null if there is no currently selected item.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public new ImageComboBoxItem SelectedItem
		{
			get
			{
				return base.SelectedItem as ImageComboBoxItem;
			}
			set
			{
				base.SelectedItem = value;
			}
		}

		/// <summary>
		/// Select by the Tag property of an <see cref="ImageComboBoxItem"/>.
		/// </summary>
		/// <value>The selected tag.</value>
		public virtual object SelectedTag
		{
			get
			{
				if ( base.SelectedItem == null )
				{
					return null;
				}
				else
				{
					var icbi = base.SelectedItem as ImageComboBoxItem;

					return icbi == null ? base.SelectedItem : icbi.Tag;
				}
			}
			set
			{
				foreach ( var o in Items )
				{
					var icbi = o as ImageComboBoxItem;

					if ( icbi == null )
					{
						if ( o == value )
						{
							base.SelectedItem = o;
							return;
						}
					}
					else
					{
						var tag = icbi.Tag;

						if ( tag == null && value == null )
						{
							base.SelectedItem = o;
							return;
						}
						else if ( tag != null && value != null )
						{
							// http://www.codeproject.com/dotnet/DotNetEquality.asp.
							if ( tag.Equals( value ) )
							{
								base.SelectedItem = o;
								return;
							}
						}
					}
				}

				// Not found.
				base.SelectedIndex = -1;
			}
		}

		// ------------------------------------------------------------------
		#endregion

		#region Private methods.
		// ------------------------------------------------------------------

		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"></see> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"></see> 
		/// that contains the event data.</param>
		protected override void OnDrawItem(
			DrawItemEventArgs e )
		{
			base.OnDrawItem( e );

			e.DrawBackground();

			drawMyStuff( e );

			if ( Enabled )
			{
				e.DrawFocusRectangle();
			}
		}

		/// <summary>
		/// Draws my stuff.
		/// </summary>
		/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> 
		/// instance containing the event data.</param>
		private void drawMyStuff(
			DrawItemEventArgs e )
		{
			// Check if it is an item from the Items collection.
			if ( e.Index < 0 )
			{
				// not an item, draw the text (indented)
				e.Graphics.DrawString(
					Text,
					e.Font,
					new SolidBrush( e.ForeColor ),
					0 + e.Bounds.Left + _imageList.ImageSize.Width,
					e.Bounds.Top );
			}
			else
			{
				// Check if item is an ImageComboBoxItem.
				if ( Items[e.Index].GetType() == typeof( ImageComboBoxItem ) )
				{
					// get item to draw
					var item = (ImageComboBoxItem)Items[e.Index];

					// get forecolor & font
					Color foreColor;

					if ( Enabled )
					{
						if ( (e.State & DrawItemState.Selected) != 0 )
						{
							// If selected, use default color to have
							// enough contrast. TreeView and ListView do the
							// same.
							foreColor = e.ForeColor;
						}
						else
						{
							foreColor =
								(item.ForeColor != Color.Transparent &&
									item.ForeColor != Color.Empty)
									? item.ForeColor
									: e.ForeColor;
						}
					}
					else
					{
						foreColor = SystemColors.GrayText;
					}

					var font = e.Font;

					if ( item.Bold || item.Italic )
					{
						if ( item.Bold && item.Italic )
						{
							font = new Font( e.Font, FontStyle.Bold | FontStyle.Italic );
						}
						else if ( item.Bold )
						{
							font = new Font( e.Font, FontStyle.Bold );
						}
						else
						{
							font = new Font( e.Font, FontStyle.Italic );
						}
					}

					// -1: no image
					if ( item.ImageIndex != -1 ||
						!string.IsNullOrEmpty( item.ImageKey ) )
					{
						var imageIndex = item.ImageIndex;

						if ( item.ImageIndex == -1 )
						{
							Debug.Assert(
								!string.IsNullOrEmpty( item.ImageKey ) );

							imageIndex =
								ImageList.Images.IndexOfKey( item.ImageKey );
							Debug.Assert( imageIndex >= 0 );
						}

						if ( imageIndex >= 0 && imageIndex < ImageList.Images.Count )
						{
							// draw image, then draw text next to it.
							ImageList.Draw(
								e.Graphics,
								item.IndentInPixel + e.Bounds.Left,
								e.Bounds.Top,
								imageIndex );
						}

						TextRenderer.DrawText(
							e.Graphics,
							item.Text,
							font,
							new Point(
								item.IndentInPixel + e.Bounds.Left + _imageList.ImageSize.Width,
								e.Bounds.Top ),
							foreColor );
					}
					else if ( item.Image != null )
					{
						// draw image, then draw text next to it.

						e.Graphics.DrawImage(
							item.Image,
							item.IndentInPixel + e.Bounds.Left,
							e.Bounds.Top,
							item.Image.Width,
							item.Image.Height );

						TextRenderer.DrawText(
							e.Graphics,
							item.Text,
							font,
							new Point(
								item.IndentInPixel + e.Bounds.Left + item.Image.Width,
								e.Bounds.Top ),
							foreColor );
					}
					else
					{
						// draw text (indented)

						var imageListImageSizeWidth = 0;
						if ( _imageList != null && !_imageList.Images.Empty )
						{
							imageListImageSizeWidth = _imageList.ImageSize.Width;
						}

						TextRenderer.DrawText(
							e.Graphics,
							item.Text,
							font,
							new Point(
								item.IndentInPixel + e.Bounds.Left + imageListImageSizeWidth,
								e.Bounds.Top ),
							foreColor );
					}
				}
				else
				{
					// it is not an ImageComboBoxItem, draw it

					TextRenderer.DrawText(
						e.Graphics,
						Items[e.Index].ToString(),
						e.Font,
						new Point(
							0 + e.Bounds.Left + _imageList.ImageSize.Width,
							e.Bounds.Top ),
						e.ForeColor
						);
				}
			}
		}

		// ------------------------------------------------------------------
		#endregion

		#region Private variables.
		// ------------------------------------------------------------------

		private ImageList _imageList = new ImageList();
		//private readonly bool _isVista;

		// ------------------------------------------------------------------
		#endregion
	}

	/////////////////////////////////////////////////////////////////////////
}

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
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions