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

An easy way to add a Ribbon Panel Office 2007 style

Rate me:
Please Sign up or sign in to vote.
4.93/5 (252 votes)
18 Apr 2007CPOL3 min read 829.9K   26.7K   521  
GNU code of RibbonPanel, free software for personal and nonlucrative use, for rest please contact me
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 TabStripApp {

    [Serializable]
    internal class TabToolboxItem : ToolboxItem {
        public TabToolboxItem() { }

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

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

        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;
                    }
                    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);
                    }

                    Dictionary<string, object> propertyValues = new Dictionary<string, object>();
                    propertyValues["Location"] = new Point(tabStrip.Left, tabStrip.Bottom + 3);

                    SetProperties(controlSwitcher, propertyValues, host);
                    


                    

                }
                finally {
                    if (t != null)
                        t.Commit();
                }

                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;
        }


        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 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
Software Developer Expediteapps
Spain Spain
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

Deeply involved in Xamarin Forms LOB (including Azure Cloud with offline support, custom controls, dependencies) projects, WP8.1 & W10 projects, WPF modern styled projects. Portable libraries like portablePDF, portableOneDrive, portableReports and portablePrinting (using Google Printing API).


Web and apps showcase at:
Expediteapps


Take a look to my blog
Blog

Comments and Discussions