WindowsFormsComponentEditor Class

WindowsFormsComponentEditor Class


Provides a base class for editors that use a modal dialog to display a properties page similar to an ActiveX control's property page.

Namespace:   System.Windows.Forms.Design
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public abstract class WindowsFormsComponentEditor : ComponentEditor


Initializes a new instance of the WindowsFormsComponentEditor class.

System_CAPS_pubmethodEditComponent(ITypeDescriptorContext, Object)

Creates an editor window that allows the user to edit the specified component, using the specified context information.(Overrides ComponentEditor.EditComponent(ITypeDescriptorContext, Object).)

System_CAPS_pubmethodEditComponent(ITypeDescriptorContext, Object, IWin32Window)

Creates an editor window that allows the user to edit the specified component.


Edits the component and returns a value indicating whether the component was modified.(Inherited from ComponentEditor.)

System_CAPS_pubmethodEditComponent(Object, IWin32Window)

Creates an editor window that allows the user to edit the specified component, using the specified window that owns the component.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Gets the component editor pages associated with the component editor.


Serves as the default hash function. (Inherited from Object.)


Gets the index of the initial component editor page for the component editor to display.


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string that represents the current object.(Inherited from Object.)

The following code example demonstrates an example WindowsFormsComponentEditor implementation.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Windows.Forms.Design;

// This example demonstrates how to implement a component editor that hosts 
// component pages and associate it with a component. This example also 
// demonstrates how to implement a component page that provides a panel-based 
// control system and Help keyword support.
namespace ComponentEditorExample
    // The ExampleComponentEditor displays two ExampleComponentEditorPage pages.
    public class ExampleComponentEditor : System.Windows.Forms.Design.WindowsFormsComponentEditor
        // This method override returns an type array containing the type of 
        // each component editor page to display.
        protected override Type[] GetComponentEditorPages()
            return new Type[] { typeof(ExampleComponentEditorPage), 
                                typeof(ExampleComponentEditorPage) }; 

        // This method override returns the index of the page to display when the 
        // component editor is first displayed.
        protected override int GetInitialComponentEditorPageIndex()
            return 1; 

    // This example component editor page type provides an example 
    // ComponentEditorPage implementation.
    internal class ExampleComponentEditorPage : System.Windows.Forms.Design.ComponentEditorPage
        Label l1; 
        Button b1; 
        PropertyGrid pg1;

        // Base64-encoded serialized image data for the required component editor page icon.

        public ExampleComponentEditorPage()
            // Initialize the page, which inherits from Panel, and its controls.
            this.Size = new Size( 400, 250 );            
            this.Icon = DeserializeIconFromBase64Text(icon);
            this.Text = "Example Page";

            b1 = new Button();
            b1.Size = new Size(200, 20);
            b1.Location = new Point(200, 0);
            b1.Text = "Set a random background color";
            b1.Click += new EventHandler(this.randomBackColor);
            this.Controls.Add( b1 );

            l1 = new Label();
            l1.Size = new Size(190, 20);
            l1.Location = new Point(4, 2);
            l1.Text = "Example Component Editor Page";
            this.Controls.Add( l1 );

            pg1 = new PropertyGrid();
            pg1.Size = new Size(400, 280);
            pg1.Location = new Point(0,30);
            this.Controls.Add( pg1 );

        // This method indicates that the Help button should be enabled for this 
        // component editor page.
        public override bool SupportsHelp()
            return true; 

        // This method is called when the Help button for this component editor page is pressed.
        // This implementation uses the IHelpService to show the Help topic for a sample keyword.
        public override void ShowHelp()
            // The GetSelectedComponent method of a ComponentEditorPage retrieves the
            // IComponent associated with the WindowsFormsComponentEditor.
            IComponent selectedComponent = this.GetSelectedComponent();

            // Retrieve the Site of the component, and return if null.
            ISite componentSite = selectedComponent.Site;
            if(componentSite == null)

            // Acquire the IHelpService to display a help topic using a indexed keyword lookup.
            IHelpService helpService = (IHelpService)componentSite.GetService(typeof(IHelpService));
            if (helpService != null)

        // The LoadComponent method is raised when the ComponentEditorPage is displayed.
        protected override void LoadComponent()
            this.pg1.SelectedObject = this.Component; 

        // The SaveComponent method is raised when the WindowsFormsComponentEditor is closing 
        // or the current ComponentEditorPage is closing.
        protected override void SaveComponent()

        // If the associated component is a Control, this method sets the BackColor to a random color.
        // This method is invoked by the button on this ComponentEditorPage.
        private void randomBackColor(object sender, EventArgs e)
            if( typeof(System.Windows.Forms.Control).IsAssignableFrom( this.Component.GetType() ) )
                // Sets the background color of the Control associated with the
                // WindowsFormsComponentEditor to a random color.
                Random rnd = new Random();
                ((System.Windows.Forms.Control)this.Component).BackColor = 
                    Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));

        // This method can be used to retrieve an Icon from a block 
        // of Base64-encoded text.
        private Icon DeserializeIconFromBase64Text(string text)
            Icon img = null;
            byte[] memBytes = Convert.FromBase64String(text);
            IFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream(memBytes);
            img = (Icon)formatter.Deserialize(stream);
            return img;

    // This example control is associated with the ExampleComponentEditor 
    // through the following EditorAttribute.
    [EditorAttribute(typeof(ExampleComponentEditor), typeof(ComponentEditor))]
    public class ExampleUserControl : System.Windows.Forms.UserControl

for full access to system resources. Demand value: InheritanceDemand. Associated state:

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft