Click here to Skip to main content
15,886,799 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.9K   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;
using System.Collections.Generic;
using System.Drawing.Design;
using System.Text;
using System.Windows.Forms;

namespace IP.Components
{
    partial class HostToolbox
    {
        /// <summary>
        /// Provides an implementation of the <see cref="IToolboxService"/> based on the Microsoft's <see cref="ToolboxService"/>.
        /// </summary>
        public class TBService : ToolboxService
        {
            private HostToolbox _toolbox;
            private static Type _itemType = typeof(Item);

            /// <summary>
            /// Initializes a new instance of the <see cref="TBService"/> with the specified <paramref name="toolbox"/> reference.
            /// </summary>
            /// <param name="toolbox">A <see cref="HostToolbox"/> component to associate.</param>
            public TBService(HostToolbox toolbox)
            {
                _toolbox = toolbox;
            }

            /// <summary>
            /// Gets a collection of strings depicting available categories of the toolbox.
            /// </summary>
            protected override CategoryNameCollection CategoryNames
            {
                get
                {
                    List<string> names = new List<string>();
                    foreach (Tab tab in _toolbox.Categories)
                    {
                        if (tab.Visible)
                            names.Add(tab.Text);
                    }
                    return new CategoryNameCollection(names.ToArray());
                }
            }

            /// <summary>
            /// Returns an <see cref="IList"/> containing all items in a given category.
            /// </summary>
            /// <param name="categoryName">The category for which to retrieve the item container list.</param>
            /// <returns>An <see cref="IList"/> containing all items in the category specified by <paramref name="categoryName"/>.</returns>
            protected override IList GetItemContainers(string categoryName)
            {
                Tab tab = _toolbox.Categories[categoryName];
                if (tab == null)
                    throw new ArgumentException("categoryName");

                return new ToolboxItemList(tab);
            }

            /// <summary>
            /// Returns an <see cref="IList"/> containing all items on the toolbox.
            /// </summary>
            /// <returns>An <see cref="IList"/> containing all items on the toolbox.</returns>
            protected override IList GetItemContainers()
            {
                return new ToolboxItemList(_toolbox);
            }

            /// <summary>
            /// Refreshes the state of the toolbox items.
            /// </summary>
            protected override void Refresh()
            {
                _toolbox.Refresh();
            }

            /// <summary>
            /// Gets or sets the name of the currently selected category.
            /// </summary>
            protected override string SelectedCategory
            {
                get
                {
                    return _toolbox.SelectedCategory;
                }
                set
                {
                    _toolbox.SelectedCategory = value;
                }
            }

            /// <summary>
            /// Gets or sets the currently selected item container.
            /// </summary>
            protected override ToolboxItemContainer SelectedItemContainer
            {
                get
                {
                    ToolboxItem item = _toolbox.GetSelectedToolboxItem();
                    if (item == null)
                        return null;
                    return new ToolboxItemContainer(item);
                }
                set
                {
                    _toolbox.SetSelectedToolboxItem((value == null) ? null : value.GetToolboxItem(null));
                }
            }

            /// <summary>
            /// Sets the current application's cursor to a cursor that represents the currently selected tool.
            /// </summary>
            /// <returns><b>true</b> if there is an item selected; otherwise, <b>false</b>.</returns>
            protected override bool SetCursor()
            {
                return _toolbox.SetCursor();
            }

            /// <summary>
            /// Creates a new toolbox item container from a saved data object.
            /// </summary>
            /// <param name="dataObject">A data object containing saved toolbox data.</param>
            /// <returns>A new toolbox item container.</returns>
            protected override ToolboxItemContainer CreateItemContainer(IDataObject dataObject)
            {
                HostItem item = _toolbox.GetDragDropTool(dataObject) as HostItem;
                if (item != null)
                {
                    ToolboxItemContainer container = CreateItemContainer(item.ToolboxItem, null);
                    return container;
                }

                return base.CreateItemContainer(dataObject);
            }

            /// <summary>
            /// Returns a value indicating whether the given data object represents an item container.
            /// </summary>
            /// <param name="dataObject">The data object to examine for the presence of a toolbox item container.</param>
            /// <param name="host">An optional designer host. This parameter can be a null reference.</param>
            /// <returns><b>true</b> if the given data object represents an item container; otherwise, <b>false</b>.</returns>
            protected override bool IsItemContainer(IDataObject dataObject, System.ComponentModel.Design.IDesignerHost host)
            {
                return base.IsItemContainer(dataObject, host);
            }

            /// <summary>
            /// Receives a call from the toolbox service when a user reports that a selected toolbox item has been used.
            /// </summary>
            protected override void SelectedItemContainerUsed()
            {
                if ((ModifierKeys & Keys.Control) != Keys.None)
                    return;

                base.SelectedItemContainerUsed();
            }
        }
    }
}

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