Click here to Skip to main content
11,410,118 members (53,212 online)
Click here to Skip to main content
Add your own
alternative version

An easy way to add a Ribbon Panel Office 2007 style

, 18 Apr 2007 CPOL
GNU code of RibbonPanel, free software for personal and nonlucrative use, for rest please contact me
demo.zip
TabStripControlLibrary.dll
diagram.png
Example.exe
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;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;

namespace RibbonStyle {
    class TabPageSwitcherDesigner : ParentControlDesigner {

        private DesignerVerbCollection verbs;  // a collection of actions to perform (appear as links in propgrid, on designer action panel)
        private ISelectionService selectionService;  // service which lets you know when the selection changes in the designer.

        /// <summary>
        /// The TabPageSwitcher we're designing - strongly typed wrapper around Component property.
        /// </summary>
        public TabPageSwitcher ControlSwitcher  {
            get { return Component as TabPageSwitcher; }
        }

        /// <summary>
        /// Fetches the selection service from the service provider - from this we can tell what's selected and when selection changes
        /// </summary>
        internal ISelectionService SelectionService {
            get {
                if (selectionService == null) {
                    selectionService = (ISelectionService)GetService(typeof(ISelectionService));
                    Debug.Assert(selectionService != null, "Failed to get Selection Service!");
                }
                return selectionService;
            }
        }

        /// <summary>
        /// List of "verbs" or actions to be used in the designer.  These typically appear on the Context Menu,
        /// links on the property grid, and as links on the designer action panel.
        /// </summary>
        public override System.ComponentModel.Design.DesignerVerbCollection Verbs {
            get {
                if (verbs == null) {

                    verbs = new DesignerVerbCollection();
                    verbs.Add(new DesignerVerb(TabStripControlLibrary.Properties.Resources.AddTab, new EventHandler(this.OnAdd)));         
                }

                return verbs;
            }
        }

        /// <summary>
        /// when the designer disposes, we need to be careful about
        /// unhooking from service events we've subscribed to.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing) {
            base.Dispose(disposing);
            if (disposing) {
                SelectionService.SelectionChanged -= new EventHandler(SelectionService_SelectionChanged);
            }
        }

        /// <summary>
        /// This is called when the designer is first loaded.  
        /// Usually a good time to hook up to events.  If you want to 
        /// set property defaults, InitializeNewComponent is what you
        /// want to override
        /// </summary>
        /// <param name="component"></param>
        public override void Initialize(IComponent component) {
            base.Initialize(component);
            SelectionService.SelectionChanged += new EventHandler(SelectionService_SelectionChanged);
        }

        public override bool CanParent(Control control) {
            return control is TabStripPage;            
        }
        /// <summary>
        /// Method implementation for our "Add TabStripPage verb".
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eevent"></param>
        private void OnAdd(object sender, EventArgs eevent) {

            // fetch our designer host
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            if (host != null) {

                // Create a transaction so we're friendly to undo/redo and serialization
                DesignerTransaction t = null;
                try {
                    try {
                        t = host.CreateTransaction(TabStripControlLibrary.Properties.Resources.AddTab + Component.Site.Name);
                    }
                    catch (CheckoutException ex) {
                        if (ex == CheckoutException.Canceled) {
                            return;
                        }
                        throw ex;
                    }

                    // Add a TabStripPage to the controls collection of the TabPageSwitcher
                    
                    // Notify the TabPageSwitcher that it's control collection is changing.                    
                    MemberDescriptor member = TypeDescriptor.GetProperties(ControlSwitcher)["Controls"];
                    TabStripPage page = host.CreateComponent(typeof(TabStripPage)) as TabStripPage;
                    RaiseComponentChanging(member);
                   
                    // add the page to the controls collection.
                    ControlSwitcher.Controls.Add(page);

                    // set the SelectedTabStripPage to the current page so that it opens correctly
                    SetProperty("SelectedTabStripPage", page);                    
                    
                    // Raise event that we're done changing the controls property.
                    RaiseComponentChanged(member, null, null);

                    // if we have an associated TabStrip,
                    // add a matching Tab to the TabStrip.
                    if (ControlSwitcher.TabStrip != null) {

                        // add a tab to the toolstrip designer
                        MemberDescriptor itemsProp = TypeDescriptor.GetProperties(ControlSwitcher.TabStrip)["Items"];

                        Tab tab = host.CreateComponent(typeof(Tab)) as Tab;
                        RaiseComponentChanging(itemsProp);

                        ControlSwitcher.TabStrip.Items.Add(tab);
                        RaiseComponentChanged(itemsProp, null, null);
                        
                        SetProperty(tab, "DisplayStyle", ToolStripItemDisplayStyle.ImageAndText);
                        SetProperty(tab, "Text", tab.Name);
                        SetProperty(tab, "TabStripPage", page);
                        SetProperty(ControlSwitcher.TabStrip, "SelectedTab", tab);
                    }
                    
                }
                finally {
                    if (t != null)
                        t.Commit();
                }
            }
         
        }

      
      
        void SelectionService_SelectionChanged(object sender, EventArgs e) {
            IList selectedComponents = (IList)SelectionService.GetSelectedComponents();
            if (selectedComponents.Count == 1) {
                Tab tab = selectedComponents[0] as Tab;
                if (tab != null) {
                    SetProperty("SelectedTabStripPage", tab.TabStripPage);
                    SetProperty(tab, "Checked", true);
                }
            }
        }
        private void SetProperty(object target, string propname, object value) {
            PropertyDescriptor propDescriptor = TypeDescriptor.GetProperties(target)[propname];
            if (propDescriptor != null) {
                propDescriptor.SetValue(target, value);
            }
        }
 
        private void SetProperty(string propname, object value) {
            SetProperty(ControlSwitcher, propname, value);
        }
    }
}

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)

Share

About the Author

Juan Pablo G.C.
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
Follow on   Twitter

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