Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

Command Switchboard for Windows Forms

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
12 Jul 2007CPOL8 min read 73.3K   1.4K   93  
Switchboard component for user interface commands with design time support
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace SundbySoft.Controls
{

    /// <summary>
    /// Represents the collection of commands in a <see cref="UISwitchboard"/>.
    /// </summary>
    public sealed class ApplicationCommandCollection : KeyedCollection<string, ApplicationCommand>
    {
        private UISwitchboard switchboard;

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationCommandCollection"/> class.
        /// </summary>
        /// <param name="UISwitchboard">The <see cref="UISwitchboard"/> that owns the collection.</param>
        internal ApplicationCommandCollection(UISwitchboard UISwitchboard)
        {
           switchboard = UISwitchboard;
        }

        /// <summary>
        /// Adds an object to the end of the Collection.
        /// </summary>
        /// <param name="item">The object to be added to the end of the Collection.</param>
        public new void Add(ApplicationCommand item)
        {
            if (item != null)
            {
                if (!Contains(item))
                {
                    item.UISwitchboard = switchboard;
                    base.Add(item);
                }
            }
        }

        /// <summary>
        /// Adds the objects of the specified collection to the end of the Collection.
        /// </summary>
        /// <param name="items">The collection whose objects should be added to the end of the Collection.</param>
        public void AddRange(ApplicationCommand[] items)
        {
            if (items != null)
            {
                foreach (ApplicationCommand item in items)
                {
                    if (item != null)
                    {
                        if (!Contains(item))
                        {
                            item.UISwitchboard = switchboard;
                            base.Add(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Inserts an element into the Collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert.</param>
        public new void Insert(int index, ApplicationCommand item)
        {
            if (item != null)
            {
                if (!Contains(item))
                {
                    item.UISwitchboard = switchboard;
                    base.Insert(index, item);
                }
            }
        }

        /// <summary>
        /// Extracts the key from the specified element.
        /// </summary>
        /// <param name="item">The element with the key.</param>
        /// <returns>The key for the specified element</returns>
        protected override string GetKeyForItem(ApplicationCommand item)
        {
            return item.ApplicationCommandName;
        }

        /// <summary>
        /// Gets the command switchboard containing the collection.
        /// </summary>
        /// <value>The element's switchboard owner.</value>
        public UISwitchboard UISwitchboard
        {
            get
            {
                return switchboard;
            }
        }
    }
}

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
Founder Sundby Software
Norway Norway
Bjørn has developed software since 1984 mainly in C, C++ and C#.

Bjørn lives in Ormåsen, Buskerud in Norway. To contact Bjørn, email him at bjsundby@online.no. He also has a web site at http://www.sundbysoft.com.

Comments and Discussions