Export (0) Print
Expand All

ControlDesigner Class

Extends the design mode behavior of a Control.

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

public class ControlDesigner : ComponentDesigner

ControlDesigner provides a base class for designers of components that derive from Control. In addition to the methods and functionality inherited from the ComponentDesigner class, ControlDesigner provides additional methods to support extending and altering the behavior of an associated Control at design time.

You can associate a designer with a type using a DesignerAttribute. For an overview of customizing design time behavior, see Extending Design-Time Support.

The following example ControlDesigner implementation demonstrates handling MouseEnter and MouseLeave events, drawing on a control from designer code, and using part of the IDesignerFilter interface to add a property for the control at design time. The following sample code contains a designer and a sample user control associated with the designer. To build this sample, compile the sample into a class library, add a reference to the library to a Windows Forms project, add the control to the Toolbox, and add an instance of the control to your form. When you point to the control, the inner outline of the perimeter of the control is highlighted, and the color used to draw the outline corresponds to the OutlineColor property that the designer has added to the properties listed for the control.

Add a reference to the System.Design assembly to compile the code example.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ControlDesignerExample
{
    // ExampleControlDesigner is an example control designer that  
    // demonstrates basic functions of a ControlDesigner.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
    public class ExampleControlDesigner  : System.Windows.Forms.Design.ControlDesigner
    {
        // This boolean state reflects whether the mouse is over the control. 
        private bool mouseover = false;
        // This color is a private field for the OutlineColor property. 
        private Color lineColor = Color.White;

        // This color is used to outline the control when the mouse is  
        // over the control. 
        public Color OutlineColor
        {
            get
            {
                return lineColor;
            }
            set
            {
                lineColor = value;
            }
        }

        public ExampleControlDesigner()
        {
        }

        // Sets a value and refreshes the control's display when the  
        // mouse position enters the area of the control. 
        protected override void OnMouseEnter()
        {
            this.mouseover = true;
            this.Control.Refresh();
        }    

        // Sets a value and refreshes the control's display when the  
        // mouse position enters the area of the control.         
        protected override void OnMouseLeave()
        {
            this.mouseover = false;            
            this.Control.Refresh();
        }        

        // Draws an outline around the control when the mouse is  
        // over the control.     
        protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe)
        {
            if (this.mouseover)
            {
                pe.Graphics.DrawRectangle(
                    new Pen(new SolidBrush(this.lineColor), 6), 
                    0, 
                    0, 
                    this.Control.Size.Width, 
                    this.Control.Size.Height);
            }
        }

        // Adds a property to this designer's control at design time  
        // that indicates the outline color to use.  
        // The DesignOnlyAttribute ensures that the OutlineColor 
        // property is not serialized by the designer. 
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            PropertyDescriptor pd = TypeDescriptor.CreateProperty(
                typeof(ExampleControlDesigner), 
                "OutlineColor",
                typeof(System.Drawing.Color),
                new Attribute[] { new DesignOnlyAttribute(true) });

            properties.Add("OutlineColor", pd);
        }
    }

    // This example control demonstrates the ExampleControlDesigner.
    [DesignerAttribute(typeof(ExampleControlDesigner))]
    public class ExampleControl : System.Windows.Forms.UserControl
    {        
        private System.ComponentModel.Container components = null;

        public ExampleControl()
        {
            components = new System.ComponentModel.Container();
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if( components != null )
                components.Dispose();
            }
            base.Dispose( disposing );
        }
    }
}

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft