Export (0) Print
Expand All

ITypeDescriptorFilterService Interface

Provides an interface to modify the set of member descriptors for a component in design mode.

Namespace:  System.ComponentModel.Design
Assembly:  System (in System.dll)

public interface ITypeDescriptorFilterService

The ITypeDescriptorFilterService type exposes the following members.

  NameDescription
Public methodFilterAttributesFilters the attributes that a component exposes through a TypeDescriptor.
Public methodFilterEventsFilters the events that a component exposes through a TypeDescriptor.
Public methodFilterPropertiesFilters the properties that a component exposes through a TypeDescriptor.
Top

The ITypeDescriptorFilterService interface provides an interface that allows modification of the properties, events, and class-level attributes of a component at design time. This modification occurs through the set of descriptors that a component provides through a TypeDescriptor. The type descriptor will query a component's site for the ITypeDescriptorFilterService service and, if it exists, the type descriptor will pass all metadata it has collected to this service. The service can then modify the metadata by adding, removing, and altering existing characteristics of the component.

For example, the properties of a component can be modified through a call to the FilterProperties method. The filter service obtains a dictionary that contains property names and their property descriptors of type PropertyDescriptor. The modifications are implemented using the following logic.

Modification

Implementation

Removal

Delete the corresponding entry in the dictionary.

Addition

Add the appropriate entry to the dictionary.

Alteration

Call existing property descriptor methods, replace the associated property descriptor entry, or replace the entire property key/value pair in the dictionary.

The return value of FilterProperties determines if this set of properties is fixed. If this method returns true, the TypeDescriptor for this component can cache the results. This cache is maintained until either the component is garbage collected or the Refresh method of the type descriptor is called.

Notes to Implementers

To filter the member descriptors exposed by a TypeDescriptor, implement this interface on a component and override the FilterAttributes, FilterEvents, or FilterProperties methods of this class to filter attributes, events, or properties, respectively.

The following code example demonstrates a designer that uses the ITypeDescriptorFilterService to filter the attributes collection of any new or existing Button to add a designer attribute for a new designer before loading or reloading the designer for the button.

To use the example, add the code to a Windows Forms project and load the components from the class library into the Toolbox.

Walkthrough: Automatically Populating the Toolbox with Custom Components
Walkthrough: Automatically Populating the Toolbox with Custom Components
Walkthrough: Automatically Populating the Toolbox with Custom Components
Walkthrough: Automatically Populating the Toolbox with Custom Components
Walkthrough: Automatically Populating the Toolbox with Custom Components

Add some buttons to your form. Add a ButtonDesignerFilterComponent to your form, and it will appear in the component tray. The ButtonDesignerFilterComponent will add a ButtonDesignerFilterService, which implements ITypeDescriptorFilterService, as a design mode service provider. Existing or new Button objects on the form will begin color cycling after the ButtonDesignerFilterService replaces the designer for each existing and new button with a ColorCycleButtonDesigner. The buttons will color cycle when you move the mouse pointer over them, and alternately continue cycling or reset the background and foreground colors on MouseLeave events. New Button objects will be given the ColorCycleButtonDesigner through the FilterAttributes method of the ButtonDesignerFilterService that has been loaded, until the ButtonDesignerFilterComponent is disposed and replaces the original ITypeDescriptorFilterService. The ColorCycleButton class in this example is a Button associated with a ColorCycleButtonDesigner.

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Reflection;
using System.Timers;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ITypeDescriptorFilterSample
{	
    // Component to host the ButtonDesignerFilterComponentDesigner that loads the  
    // ButtonDesignerFilterService ITypeDescriptorFilterService.
    [Designer(typeof(ButtonDesignerFilterComponentDesigner))]
    public class ButtonDesignerFilterComponent : System.ComponentModel.Component
    {		
        public ButtonDesignerFilterComponent(System.ComponentModel.IContainer container)
        {
            container.Add(this);
        }

        public ButtonDesignerFilterComponent()
        {}
    }

    // Provides a designer that can add a ColorCycleButtonDesigner to each  
    // button in a design time project using the ButtonDesignerFilterService  
    // ITypeDescriptorFilterService. 
    public class ButtonDesignerFilterComponentDesigner : System.ComponentModel.Design.ComponentDesigner
    {
        // Indicates whether the service has been loaded. 
        private bool serviceloaded = false;		
        // Stores any old ITypeDescriptorFilterService to restore later. 
        private ITypeDescriptorFilterService oldservice = null;

        public ButtonDesignerFilterComponentDesigner()
        {}

        // Loads the new ITypeDescriptorFilterService and reloads the  
        // designers for each button. 
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            // Loads the custom service if it has not been loaded already
            LoadService();

            // Build list of buttons from Container.Components.
            ArrayList buttons = new ArrayList();
            foreach(IComponent c in this.Component.Site.Container.Components)
                if(c.GetType() == typeof(System.Windows.Forms.Button))
                    buttons.Add((System.Windows.Forms.Button)c);
            if(buttons.Count > 0)
            {		
                // Tests each Button for an existing  
                // ColorCycleButtonDesigner; 
                // if it has no designer of type  
                // ColorCycleButtonDesigner, adds a designer. 
                foreach(System.Windows.Forms.Button b in buttons)
                {
                    bool loaddesigner = true;
                    // Gets the attributes for each button.
                    AttributeCollection ac = TypeDescriptor.GetAttributes(b);
                    for(int i=0; i<ac.Count; i++)
                    {
                        // If designer attribute is not for a  
                        // ColorCycleButtonDesigner, adds a new  
                        // ColorCycleButtonDesigner. 
                        if( ac[i] is DesignerAttribute )
                        {
                            DesignerAttribute da = (DesignerAttribute)ac[i];
                            if( da.DesignerTypeName.Substring(da.DesignerTypeName.LastIndexOf(".")+1) == "ColorCycleButtonDesigner" )
                                loaddesigner = false;
                        }
                    }
                    if(loaddesigner)
                    {
                        // Saves the button location so that it  
                        // can be repositioned.
                        Point p = b.Location;	

                        // Gets an IMenuCommandService to cut and  
                        // paste control in order to register with  
                        // selection and movement interface after  
                        // designer is changed without reloading.
                        IMenuCommandService imcs = (IMenuCommandService)this.GetService(typeof(IMenuCommandService));
                        if( imcs == null )
                            throw new Exception("Could not obtain IMenuCommandService interface.");							
                        // Gets an ISelectionService to select the  
                        // button so that it can be cut and pasted.
                        ISelectionService iss = (ISelectionService)this.GetService(typeof(ISelectionService));
                        if( iss == null)
                            throw new Exception("Could not obtain ISelectionService interface.");
                        iss.SetSelectedComponents(new IComponent[] { b }, SelectionTypes.Auto);						
                        // Invoke Cut and Paste.
                        imcs.GlobalInvoke(StandardCommands.Cut);
                        imcs.GlobalInvoke(StandardCommands.Paste);							
                        // Regains reference to button from  
                        // selection service.
                        System.Windows.Forms.Button b2 = (System.Windows.Forms.Button)iss.PrimarySelection;
                        iss.SetSelectedComponents(null);							
                        // Refreshes TypeDescriptor properties of  
                        // button to load new attributes from 
                        // ButtonDesignerFilterService.
                        TypeDescriptor.Refresh(b2);							
                        b2.Location = p;
                        b2.Focus();
                    }
                }
            }
        }

        // Loads a ButtonDesignerFilterService ITypeDescriptorFilterService  
        // to add ColorCycleButtonDesigner designers to each button. 
        private void LoadService()
        {
            // If no custom ITypeDescriptorFilterService is loaded,  
            // loads it now. 
            if(!serviceloaded)
            {
                // Stores the current ITypeDescriptorFilterService  
                // to restore later.
                ITypeDescriptorFilterService tdfs = (ITypeDescriptorFilterService)this.Component.Site.GetService(typeof(ITypeDescriptorFilterService));
                if( tdfs != null )								
                    oldservice = tdfs;								
                // Retrieves an IDesignerHost interface to use to  
                // remove and add services.
                IDesignerHost dh = (IDesignerHost)this.Component.Site.GetService(typeof(IDesignerHost));
                if( dh == null )
                    throw new Exception("Could not obtain IDesignerHost interface.");				
                // Removes standard ITypeDescriptorFilterService.
                dh.RemoveService(typeof(ITypeDescriptorFilterService));
                // Adds new custom ITypeDescriptorFilterService.
                dh.AddService(typeof(ITypeDescriptorFilterService), new ButtonDesignerFilterService());				
                serviceloaded = true;
            }
        }

        // Removes the custom service and reloads any stored,  
        // preexisting service. 
        private void RemoveService()
        {
            IDesignerHost dh = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            if( dh == null )
                throw new Exception("Could not obtain IDesignerHost interface.");
            dh.RemoveService(typeof(ITypeDescriptorFilterService));
            if( oldservice != null )
                dh.AddService(typeof(ITypeDescriptorFilterService), oldservice);
            serviceloaded = false;
        }

        protected override void Dispose(bool disposing)
        {
            if(serviceloaded)
                RemoveService();
        }
    }

    // Provides a TypeDescriptorFilterService to add the  
    // ColorCycleButtonDesigner using a DesignerAttribute. 
    public class ButtonDesignerFilterService : System.ComponentModel.Design.ITypeDescriptorFilterService
    {
        public ITypeDescriptorFilterService oldService = null;

        public ButtonDesignerFilterService()
        {}

        public ButtonDesignerFilterService(ITypeDescriptorFilterService oldService_)
        {
            // Stores any previous ITypeDescriptorFilterService to implement service chaining. 
            this.oldService = oldService_;
        }

        public bool FilterAttributes(System.ComponentModel.IComponent component, System.Collections.IDictionary attributes)
        {
            if(oldService != null)
                oldService.FilterAttributes(component, attributes);

            // Creates a designer attribute to compare its TypeID with the TypeID of existing attributes of the component.
            DesignerAttribute da = new DesignerAttribute(typeof(ColorCycleButtonDesigner));
            // Adds the designer attribute if the attribute collection does not contain a DesignerAttribute of the same TypeID. 
            if(component is System.Windows.Forms.Button && attributes.Contains(da.TypeId))
                attributes[da.TypeId]=da;
            return true;
        }

        public bool FilterEvents(System.ComponentModel.IComponent component, System.Collections.IDictionary events)
        {
            if(oldService != null)
                oldService.FilterEvents(component, events);
            return true;
        }

        public bool FilterProperties(System.ComponentModel.IComponent component, System.Collections.IDictionary properties)
        {
            if(oldService != null)
                oldService.FilterProperties(component, properties);
            return true;
        }
    }

    // Designer for a Button control which cycles the background color. 
    public class ColorCycleButtonDesigner : System.Windows.Forms.Design.ControlDesigner
    {
        private System.Windows.Forms.Timer timer1;
        private Color initial_bcolor, initial_fcolor;
        private int r, g, b;
        private bool ru, gu, bu, continue_;

        public ColorCycleButtonDesigner()
        {						
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 50;
            timer1.Tick += new EventHandler(this.Elapsed);
            ru = true;
            gu = false;
            bu = true;			
            continue_ = false;
            timer1.Start();
        }

        private void Elapsed(object sender, EventArgs e)
        {
            this.Control.BackColor = Color.FromArgb(r%255, g%255, b%255);
            this.Control.Refresh();

            // Updates color.		 
            if(ru)
                r+=10;
            else if(r>10) 
                r-=10;
            if(gu)
                g+=10;
            else if(g>10)
                g-=10;
            if(bu)
                b+=10;
            else if(b>10)
                b-=10;

            // Randomly switches direction of color component values.
            Random rand = new Random();
            for(int i=0; i<4; i++)			
                switch(rand.Next(0, 2))
                {
                    case 0:
                        if(ru)
                            ru=false;
                        else
                            ru=true;
                        break;
                    case 1:
                        if(gu)
                            gu=false;
                        else
                            gu=true;
                        break;
                    case 2:
                        if(bu)
                            bu=false;
                        else
                            bu=true;
                        break;
                }

            this.Control.ForeColor = Color.FromArgb((this.Control.BackColor.R+128)%255, (this.Control.BackColor.G+128)%255, (this.Control.BackColor.B+128)%255);
        }

        protected override void OnMouseEnter()
        {
            if(!timer1.Enabled)
            {
                initial_bcolor = this.Control.BackColor;
                initial_fcolor = this.Control.ForeColor;
                r = initial_bcolor.R;
                g = initial_bcolor.G;
                b = initial_bcolor.B;			
                timer1.Start();
            }
        }

        protected override void OnMouseLeave()
        {
            if(!continue_)
            {
                continue_ = true;
                timer1.Stop();
            }
            else
                continue_ = false;

            this.Control.BackColor = initial_bcolor;
            this.Control.ForeColor = initial_fcolor;			
        }

        protected override void Dispose(bool disposing)
        {
            timer1.Stop();
            this.Control.BackColor = initial_bcolor;
            this.Control.ForeColor = initial_fcolor;
            base.Dispose(disposing);
        }
    }

    // System.Windows.Forms.Button associated with the ColorCycleButtonDesigner.
    [Designer(typeof(ColorCycleButtonDesigner))]
    public class ColorCycleButton : System.Windows.Forms.Button
    {
        public ColorCycleButton()
        {}
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft