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

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 277.8K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Storm.TextEditor.Editor.CodeCompletion
{
	/// <summary>
	/// Represents a generic ListBox with icons.
	/// </summary>
    [ToolboxItem(false)]
	public class CodeCompletionListBox
		: ListBox
	{
		#region Fields

		private ICompletionDataProvider completionDataProvider = null;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the current ICompletionDataProvider of the CodeCompletionListBox.
		/// </summary>
		internal ICompletionDataProvider CompletionDataProvider
		{
			get { return completionDataProvider; }
			set
			{
				completionDataProvider = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the height of an item in the <see cref="T:System.Windows.Forms.ListBox"/>.
		/// </summary>
		/// <value></value>
		/// <returns>
		/// The height, in pixels, of an item in the control.
		/// </returns>
		/// <exception cref="T:System.ArgumentOutOfRangeException">
		/// The <see cref="P:System.Windows.Forms.ListBox.ItemHeight"/> property was set to less than 0 or more than 255 pixels.
		/// </exception>
		/// <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 override int ItemHeight
		{
			get { return Math.Max(this.CompletionDataProvider.ImageList.ImageSize.Height, (int)(this.Font.Height)); }
			set { }
		}

		#endregion

		#region Methods

		#region Protected

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

			e.DrawBackground();
			e.DrawFocusRectangle();

			ICompletionData completionData = null;
			Rectangle bounds = e.Bounds;
			Size imageSize = this.CompletionDataProvider.ImageList.ImageSize;

			try
			{
				completionData = (ICompletionData)this.Items[e.Index];
				if (completionData.ImageIndex > -1)
				{
					this.CompletionDataProvider.ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, completionData.ImageIndex);
					e.Graphics.DrawString(completionData.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
				}
				else
					e.Graphics.DrawString(completionData.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
			}
			catch
			{
				if (e.Index != -1)
					e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
				else
					e.Graphics.DrawString(this.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
			}
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of the <see cref="CodeCompletionListBox"/> class.
		/// </summary>
		public CodeCompletionListBox()
		{
			this.DrawMode = DrawMode.OwnerDrawFixed;
		}
	}
}

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 GNU Lesser General Public License (LGPLv3)



Comments and Discussions