Click here to Skip to main content
15,879,613 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 273.4K   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;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Storm.CodeCompletion
{
	/// <summary>
	/// Class inheriting the TreeNode. Acts as item for the main class, GListBox.
	/// </summary>
    public class GListBoxItem
		: TreeNode, IComparable
	{
		#region Fields

		private string        _text = "";
		private string _description = "";
		private string _declaration = "";

		private object                _tag = "";
		private int            _imageIndex = -1;
		private MemberType     _memberType = MemberType.Normal;
		private CompleteType _completeType = CompleteType.Normal;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the Text of the GListBoxItem.
		/// </summary>
		public new string Text
		{
			get { return base.Text; }
			set
			{
				this._text = value;
				base.Text = this._text;
			}
		}

		/// <summary>
		/// Gets or sets the Description of the GListBoxItem. 
		/// The Description is what is showed in the tooltip.
		/// </summary>
		public string Description
		{
			get { return this._description; }
			set { this._description = value; }
		}

		/// <summary>
		/// Gets or sets the Declaration of the GListBoxItem. 
		/// The Declaration is what is showed in the tooltip.
		/// </summary>
		public string Declaration
		{
			get { return this._declaration; }
			set { this._declaration = value; }
		}

		/// <summary>
		/// Gets or sets the ImageIndex of the GListBoxItem.
		/// </summary>
		public new int ImageIndex
		{
			get { return base.ImageIndex; }
			set
			{
				this._imageIndex = value;
				base.ImageIndex = this._imageIndex;
			}
		}

		/// <summary>
		/// Gets or sets the Tag of the GListBoxItem.
		/// </summary>
		public new object Tag
		{
			get { return base.Tag; }
			set
			{
				this._tag = value;
				base.Tag = this._tag;
			}
		}

		/// <summary>
		/// Gets or sets the MemberType of the GListBoxItem.
		/// </summary>
		public MemberType MemberType
		{
			get { return this._memberType; }
			set { this._memberType = value; }
		}

		/// <summary>
		/// Gets or sets how the GListBoxItem should complete itself.
		/// </summary>
		public CompleteType CompleteType
		{
			get { return this._completeType; }
			set { this._completeType = value; }
		}

		#endregion

		#region Methods

		#region Public

        /// <summary>
        /// Compares the GListBoxItem to another GListBoxItem.
        /// </summary>
        /// <param name="obj">Object to compare to.</param>
        /// <returns>Variable value depending on how the two GListBoxItems were alike.</returns>
        public int CompareTo(object obj)
        {
            if (((obj) is GListBoxItem))
            {
                GListBoxItem memberItem = (GListBoxItem)obj;
                return (Text.CompareTo(memberItem.Text));
            }
            else
            {
                throw new ArgumentException();
            }
        }

		/// <summary>
		/// Gets the GListBoxItem typecasted to string.
		/// </summary>
		/// <returns>GListBoxItem typecasted to string.</returns>
		public override string ToString()
		{ return this.Text; }

		#endregion

		#endregion

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		public GListBoxItem()
			: this("")
		{ }

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		public GListBoxItem(string text)
			: this(text, -1)
		{ }

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="imageIndex">Image for the GListBoxItem to display.</param>
		public GListBoxItem(string text, int imageIndex)
		{
			this.Text = text;
			this.ImageIndex = imageIndex;
		}

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="memberType">MemberType value for the GListBoxItem.</param>
		public GListBoxItem(string text, MemberType memberType)
		{
			this.Text = text;
			this.MemberType = memberType;
		}

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="imageIndex">Image for the GListBoxItem to display.</param>
		/// <param name="memberType">MemberType value for the GListBoxItem.</param>
		public GListBoxItem(string text, int imageIndex, MemberType memberType)
		{
			this.Text = text;
			this.ImageIndex = imageIndex;
			this.MemberType = memberType;
		}

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="imageIndex">Image for the GListBoxItem to display.</param>
		/// <param name="memberType">MemberType value for the GListBoxItem.</param>
		/// <param name="description">Description value for the GListBoxItem.</param>
		public GListBoxItem(string text, int imageIndex, MemberType memberType, string description)
		{
			this.Text = text;
			this.ImageIndex = imageIndex;
			this.MemberType = memberType;
			this.Description = description;
		}

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="imageIndex">Image for the GListBoxItem to display.</param>
		/// <param name="memberType">MemberType value for the GListBoxItem.</param>
		/// <param name="description">Description value for the GListBoxItem.</param>
		/// <param name="declaration">Declaration value for the GListBoxItem.</param>
		public GListBoxItem(string text, int imageIndex, MemberType memberType, 
			string description, string declaration)
		{
			this.Text = text;
			this.ImageIndex = imageIndex;
			this.MemberType = memberType;
			this.Description = description;
			this.Declaration = declaration;
		}

		/// <summary>
		/// Initializes the GListBoxItem.
		/// </summary>
		/// <param name="text">Text for the GListBoxItem to display.</param>
		/// <param name="imageIndex">Image for the GListBoxItem to display.</param>
		/// <param name="memberType">MemberType value for the GListBoxItem.</param>
		/// <param name="description">Description value for the GListBoxItem.</param>
		/// <param name="declaration">Declaration value for the GListBoxItem.</param>
		/// <param name="completeType">How the GListBoxItem should complete itself.</param>
		public GListBoxItem(string text, int imageIndex, MemberType memberType,
			string description, string declaration, CompleteType completeType)
		{
			this.Text = text;
			this.ImageIndex = imageIndex;
			this.MemberType = memberType;
			this.Description = description;
			this.Declaration = declaration;
			this.CompleteType = completeType;
		}
	}

	/// <summary>
	/// Class inheriting from the ListBox control, which consists of GListBoxItems instead of normal ListBoxItems. GListBoxItems are able to have images.
	/// </summary>
	public class GListBox
		: ListBox
	{
		#region Fields

		private ImageList _imageList;

		#endregion

		#region Properties

		/// <summary>
		/// Gets or sets the ImageList of the GListBox.
		/// </summary>
		public ImageList ImageList
		{
			get { return this._imageList; }
			set { this._imageList = value; }
		}

		#endregion

		#region Methods

		#region Protected

		/// <summary>
		/// Draws items in the GListBox.
		/// </summary>
		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();

			GListBoxItem item = default(GListBoxItem);
			Rectangle bounds = e.Bounds;
			Size imageSize = _imageList.ImageSize;
            
			try
			{
				item = (GListBoxItem)Items[e.Index];
				if (item.ImageIndex > -1)
				{
					this._imageList.Draw(e.Graphics, bounds.Left, 
						bounds.Top, item.ImageIndex);

					e.Graphics.DrawString(item.Text, e.Font, new SolidBrush
					(e.ForeColor), bounds.Left + imageSize.Width, bounds.Top);
				}
				else
				{
					e.Graphics.DrawString(item.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 the GListBox.
		/// </summary>
		public GListBox()
		{ 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