Click here to Skip to main content
15,886,823 members
Articles / Desktop Programming / Windows Forms

A Professional Ribbon You Will Use (Now with orb!)

Rate me:
Please Sign up or sign in to vote.
4.96/5 (573 votes)
12 Jun 2012Ms-PL6 min read 2.3M   141.7K   1.4K  
A serious project on an Office-like Ribbon control
/*
 
 2008 Jos� Manuel Men�ndez Poo
 * 
 * Please give me credit if you use this code. It's all I ask.
 * 
 * Contact me for more info: menendezpoo@gmail.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;

namespace System.Windows.Forms
{
    internal abstract class RibbonElementWithItemCollectionDesigner
        : ComponentDesigner
    {
        #region Props

        public abstract Ribbon Ribbon { get; }

        public abstract RibbonItemCollection Collection { get; }

        public override DesignerVerbCollection Verbs
        {
            get
            {
                return new DesignerVerbCollection(new DesignerVerb[] { 
                    new DesignerVerb("Add Button", new EventHandler(AddButton)),
                    new DesignerVerb("Add ButtonList", new EventHandler(AddButtonList)),
                    new DesignerVerb("Add ItemGroup", new EventHandler(AddItemGroup)),
                    new DesignerVerb("Add Separator", new EventHandler(AddSeparator)),
                    new DesignerVerb("Add TextBox", new EventHandler(AddTextBox)),
                    new DesignerVerb("Add ComboBox", new EventHandler(AddComboBox)),
                    new DesignerVerb("Add ColorChooser", new EventHandler(AddColorChooser))
                });
            }
        }

        #endregion

        #region Methods

        private void CreateItem(Type t)
        {
            CreateItem(Ribbon, Collection, t);
        }

        protected virtual void CreateItem(Ribbon ribbon, RibbonItemCollection collection, Type t)
        {
            IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (host != null && collection != null && ribbon != null)
            {
                DesignerTransaction transaction = host.CreateTransaction("AddRibbonItem_" + Component.Site.Name);

                MemberDescriptor member = TypeDescriptor.GetProperties(Component)["Items"];
                base.RaiseComponentChanging(member);

                RibbonItem item = host.CreateComponent(t) as RibbonItem;

                if (!(item is RibbonSeparator)) item.Text = item.Site.Name;

                collection.Add(item);
                ribbon.OnRegionsChanged();

                base.RaiseComponentChanged(member, null, null);
                transaction.Commit();
            }
        }

        protected virtual void AddButton(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonButton));
        }

        protected virtual void AddButtonList(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonButtonList));
        }

        protected virtual void AddItemGroup(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonItemGroup));
        }

        protected virtual void AddSeparator(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonSeparator));
        }

        protected virtual void AddTextBox(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonTextBox));
        }

        protected virtual void AddComboBox(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonComboBox));
        }

        protected virtual void AddColorChooser(object sender, EventArgs e)
        {
            CreateItem(typeof(RibbonColorChooser));
        }

        #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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Product Manager
United States United States
- I've been programming Windows and Web apps since 1997.
- My greatest concern nowadays is product, user interface, and usability.
- TypeScript / React expert

@geeksplainer

Comments and Discussions