Click here to Skip to main content
Click here to Skip to main content

An easy way to add a Ribbon Panel Office 2007 style

By , 18 Apr 2007
 
Prize winner in Competition "Best C# article of Mar 2007"
demo.zip
diagram.png
Example.exe
TabStripControlLibrary.dll
source.zip
Source
TabStripApp
archive
bin
Release
diagram.png
Example.exe
TabStripApp.pdb
TabStripApp.vshost.exe
TabStripControlLibrary.dll
TabStripControlLibrary.pdb
Properties
Settings.settings
Resources
advanced-directory.png
B_click.png
B_click1.png
B_click11.png
B_on.png
B_on1.png
B_on11.png
config-users.png
gnome-dev-harddisk-1394.png
gnome-dev-removable-usb.png
gnome-finance.png
gnome-netstatus-rx.png
gnome-searchtool-animation.png
gnome-starthere.png
logviewer.png
mozilla-thunderbird.png
pan.png
stock_contact.png
stock_mail-compose.png
stock_mail-druid.png
stock_mail-flag-for-followup-done.png
stock_mail-receive.png
stock_mail-receive1.png
stock_mail-send.png
stock_new-spreadsheet.png
stock_outbox.png
stock_weather-storm.png
Thumbs.db
xsane.png
TabStripControlLibrary
bin
Release
TabStripControlLibrary.dll
TabStripControlLibrary.pdb
Properties
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Design;
using System.Design;
using System.Windows.Forms.ComponentModel;
using System.Runtime.Serialization;
using System.Windows.Forms.Design;
using System.Collections.Generic;


namespace RibbonStyle {


    /// <summary>
    /// This class is invoked when dragging a TabStrip off the toolbox.
    /// note that it creates two things - a TabStrip and a TabStripPageSwitcher.
    /// </summary>
    [Serializable]
    internal class TabStripToolboxItem : ToolboxItem {
        public TabStripToolboxItem() { }

        public TabStripToolboxItem(Type toolType)
            : base(toolType) {
        }

        private TabStripToolboxItem(SerializationInfo info, StreamingContext context) {
            Deserialize(info, context);
        }


        /// <summary>
        /// this method is called when dragging a TabStrip off the toolbox.
        /// </summary>
        /// <param name="host"></param>
        /// <returns></returns>
        protected override IComponent[] CreateComponentsCore(IDesignerHost host) {
            IComponent[] components = base.CreateComponentsCore(host);
            
            Control parentControl = null;
            ControlDesigner parentControlDesigner = null;
            TabStrip tabStrip = null;
            IComponentChangeService changeSvc = (IComponentChangeService)host.GetService(typeof(IComponentChangeService));
                    

            // fish out the parent we're adding the TabStrip to.
            if (components.Length > 0 && components[0] is TabStrip) {
                tabStrip = components[0] as TabStrip;

                ITreeDesigner tabStripDesigner = host.GetDesigner(tabStrip) as ITreeDesigner;
                parentControlDesigner = tabStripDesigner.Parent as ControlDesigner;
                if (parentControlDesigner != null) {
                    parentControl = parentControlDesigner.Control;
                }               
            }
           
            // Create a ControlSwitcher on the same parent.

            if (host != null) {
                TabPageSwitcher controlSwitcher = null;

                DesignerTransaction t = null;
                try {
                    try {
                        t = host.CreateTransaction("add tabswitcher");
                    }
                    catch (CheckoutException ex) {
                        if (ex == CheckoutException.Canceled) {
                            return components;
                        }
                        throw ex;
                    }

                    // create a TabPageSwitcher and add it to the same parent as the TabStrip
                    MemberDescriptor controlsMember = TypeDescriptor.GetProperties(parentControlDesigner)["Controls"];
                    controlSwitcher = host.CreateComponent(typeof(TabPageSwitcher)) as TabPageSwitcher;
                    
                    if (changeSvc != null) {
                        changeSvc.OnComponentChanging(parentControl, controlsMember);                           
                        changeSvc.OnComponentChanged(parentControl, controlsMember, null, null);
                    }

                    // specify default values for our TabStrip
                    Dictionary<string, object> propertyValues = new Dictionary<string, object>();
                    propertyValues["Location"] = new Point(tabStrip.Left, tabStrip.Bottom + 3);
                    propertyValues["TabStrip"] = tabStrip;

                    // set the property values
                    SetProperties(controlSwitcher, propertyValues, host);
                    
                }
                finally {
                    if (t != null)
                        t.Commit();
                }

                // add the TabPageSwitcher to the list of components that we've created
                if (controlSwitcher != null) {
                    IComponent[] newComponents = new IComponent[components.Length + 1];
                    Array.Copy(components, newComponents, components.Length);
                    newComponents[newComponents.Length - 1] = controlSwitcher;
                    return newComponents;
                }

            }

            return components;
        }


        /// <summary>
        ///  handy helper method for setting multiple properties. 
        /// </summary>
        /// <param name="component"></param>
        /// <param name="properties"></param>
        /// <param name="host"></param>
        private void SetProperties(Component component, Dictionary<string,object> properties, IDesignerHost host) {
            IComponentChangeService changeSvc = (IComponentChangeService)host.GetService(typeof(IComponentChangeService));
                      
            foreach (string propname in properties.Keys) {
                PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(component)[propname];
           
                if (changeSvc != null) {
                    changeSvc.OnComponentChanging(component, propDescriptor);
                }
                if (propDescriptor != null) {
                    propDescriptor.SetValue(component, properties[propname]);
                }
                if (changeSvc != null) {
                    changeSvc.OnComponentChanged(component, propDescriptor, null, null);
                }
            }

            
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of use 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)

About the Author

Juan Pablo G.C.
Software Developer
Spain Spain
Member
I'm Electronic Engineer, I did my end degree project at Astrophysical Institute and Tech Institute. I'm HP Procurve AIS and ASE ,Microsoft 3.5 MCTS
I live in Canary Islands ,developing customized solutions
 
I'm developing with WPF4, SL4 MVVM, MVC3 Razor and WP7 projects, more info at my websites. Improving with Android and IOS.
 
Websites:
MemCards
Mareinsula


Take a look to my blog Juan Pablo G.C.
Mareinsula

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 18 Apr 2007
Article Copyright 2007 by Juan Pablo G.C.
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid