Click here to Skip to main content
15,885,990 members
Articles / Programming Languages / C#

IPToolbox: A VS2005-like Toolbox

Rate me:
Please Sign up or sign in to vote.
4.90/5 (50 votes)
25 May 20074 min read 231.8K   7K   162  
A Visual Studio 2005-like ToolBox control which supports almost all features of the original: drag'n'drop, renaming, hiding tabs, items, and disabling items
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace IP.Components
{
    partial class HostToolbox
    {
        /// <summary>
        /// Represents an <see cref="Toolbox.Item"/> associated with the <see cref="ToolboxItem"/> object.
        /// </summary>
        [Serializable]
        public class HostItem : Item
        {
            #region Fields
            private ToolboxItem _item;
            private bool _projectSpecific;
            #endregion

            #region Constructors
            /// <summary>
            /// Initializes a new instance of the <see cref="HostItem"/>.
            /// </summary>
            public HostItem()
            {
            }
            /// <summary>
            /// Initializes a new instance of the <see cref="HostItem"/> using the specified <see cref="ToolboxItem"/> object.
            /// </summary>
            /// <param name="item">A <see cref="ToolboxItem"/> object to associate with an item.</param>
            public HostItem(ToolboxItem item)
                : base((item != null) ? item.DisplayName : "")
            {
                _item = item;
            }

            /// <summary>
            /// Initializes a new instance of the <see cref="HostItem"/> using the specified <see cref="ToolboxItem"/> object and a value indicating whether this item is specific for the project/design surface.
            /// </summary>
            /// <param name="item">A <see cref="ToolboxItem"/> object to associate with an item.</param>
            /// <param name="projectSpecific">Specifies whether the <see cref="HostItem"/> is a project-specific item.</param>
            public HostItem(ToolboxItem item, bool projectSpecific) : this(item)
            {
                _projectSpecific = projectSpecific;
            }
            #endregion

            #region Public Properties
            /// <summary>
            /// Gets or sets the text value of the <see cref="HostItem"/>.
            /// </summary>
            [XmlIgnore]
            public override string Text
            {
                get
                {
                    if (string.IsNullOrEmpty(base.Text))
                        return _item.DisplayName;
                    return base.Text;
                }
                [DebuggerStepThrough]
                set { base.Text = value; }
            }

            /// <summary>
            /// Gets or sets the image displayed for the <see cref="HostItem"/>.
            /// </summary>
            [XmlIgnore]
            public override Image Image
            {
                get
                {
                    if (base.Image == null)
                        return _item.Bitmap;
                    return base.Image;
                }
                [DebuggerStepThrough]
                set { base.Image = value; }
            }

            /// <summary>
            /// Gets or set the tooltip text of the <see cref="HostItem"/>.
            /// </summary>
            [XmlIgnore]
            public override string Tooltip
            {
                get
                {
                    if (string.IsNullOrEmpty(base.Tooltip) || base.Tooltip == base.Text)
                        return _item.Description;
                    return base.Tooltip;
                }
                [DebuggerStepThrough]
                set { base.Tooltip = value; }
            }

            /// <summary>
            /// Gets a <see cref="ToolboxItem"/> associated with the <see cref="HostItem"/>.
            /// </summary>
            [System.ComponentModel.Browsable(false)]
            public ToolboxItem ToolboxItem
            {
                get { return _item; }
                private set
                {
                    _item = value;
                    Text = value.DisplayName;
                }
            }

            /// <summary>
            /// Indicates whether the <see cref="ToolboxItem"/> is specific for the project/design surface.
            /// </summary>
            public bool ProjectSpecific
            {
                get { return _projectSpecific; }
            }

            #endregion

            #region Public Methods
            /// <summary>
            /// Creates an identical copy of the <see cref="HostItem"/>.
            /// </summary>
            /// <returns>An object that represents an <see cref="HostItem"/> that has the same <see cref="ToolboxItem"/>, forecolor and backcolor associated with it as the cloned item.</returns>
            public override Item Clone()
            {
                HostItem hostItem = new HostItem(_item, _projectSpecific);
                hostItem.BackColor = BackColor;
                hostItem.ForeColor = ForeColor;
                hostItem.Text = base.Text;
                hostItem.Image = base.Image;
                hostItem.Tooltip = base.Tooltip;
                return hostItem;
            }
            #endregion

            #region Protected Methods
            /// <summary>
            /// Serializes a <see cref="HostItem"/> to the <see cref="XmlElement"/>.
            /// </summary>
            /// <param name="document">An <see cref="XmlDocument"/> that will own the resulting <see cref="XmlElement"/>.</param>
            /// <returns>An <see cref="XmlElement"/> that contains <see cref="HostItem"/> data.</returns>
            /// <exception cref="System.ArgumentNullException"><paramref name="document"/> is a null reference.</exception>
            protected internal override XmlElement SerializeToXml(XmlDocument document)
            {
                XmlElement xml = base.SerializeToXml(document);
                xml.SetAttribute(Serializer_TypeName, string.Format("{0}, {1}", ToolboxItem.TypeName, ToolboxItem.AssemblyName));

                if (Text != ToolboxItem.DisplayName)
                    xml.SetAttribute("Text", Text);
                if (Tooltip != ToolboxItem.Description)
                    xml.SetAttribute("Tooltip", Tooltip);
                if (Image != ToolboxItem.Bitmap)
                {
                    using (MemoryStream stream = new MemoryStream())
                    {
                        Image.Save(stream, ImageFormat.Bmp);
                        stream.Flush();
                        xml.SetAttribute("Image", Convert.ToBase64String(stream.ToArray()));
                    }
                }

                return xml;
            }

            /// <summary>
            /// Deserializes a <see cref="HostItem"/> data from the <see cref="XmlElement"/>.
            /// </summary>
            /// <param name="element">An <see cref="XmlElement"/> that contains <see cref="HostItem"/> data.</param>
            /// <exception cref="System.ArgumentNullException"><paramref name="element"/> is a null reference.</exception>
            /// <exception cref="System.ArgumentException"><paramref name="element"/> does not contain attribute with the name of the referenced type.</exception>
            /// <exception cref="System.TypeLoadException"><paramref name="element"/> contains an unknown type reference.</exception>
            protected internal override void DeserializeXml(XmlElement element)
            {
                XmlAttribute attribute = element.Attributes[Serializer_TypeName];
                if (attribute == null)
                    throw new ArgumentException();
                Type type = Type.GetType(attribute.Value);
                if (type == null)
                    throw new TypeLoadException();
                ToolboxItem = new ToolboxItem(type);

                attribute = element.Attributes["Text"];
                if (attribute != null)
                    this.Text = attribute.Value;
                attribute = element.Attributes["Tooltip"];
                if (attribute != null)
                    this.Tooltip = attribute.Value;
                attribute = element.Attributes["Image"];
                if (attribute != null)
                {
                    try
                    {
                        MemoryStream stream = new MemoryStream(Convert.FromBase64String(attribute.Value));
                        this.Image = System.Drawing.Image.FromStream(stream);
                    }
                    catch { }
                }
                base.DeserializeXml(element);
            }
            #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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
I@n
Web Developer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions