Export (0) Print
Expand All

ControlDesigner Class

Base designer class for extending the design mode behavior of a Control.

For a list of all members of this type, see ControlDesigner Members.

System.Object
   System.ComponentModel.Design.ComponentDesigner
      System.Windows.Forms.Design.ControlDesigner
         System.Windows.Forms.Design.ParentControlDesigner

[Visual Basic]
Public Class ControlDesigner
   Inherits ComponentDesigner
[C#]
public class ControlDesigner : ComponentDesigner
[C++]
public __gc class ControlDesigner : public ComponentDesigner
[JScript]
public class ControlDesigner extends ComponentDesigner

Thread Safety

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

Remarks

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 Enhancing Design-Time Support.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Collections
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace ControlDesignerExample
    _
    ' ExampleControlDesigner is an example control designer that 
    ' demonstrates basic functions of a ControlDesigner.
    Public Class TestControlDesigner
        Inherits System.Windows.Forms.Design.ControlDesigner

        ' This boolean state reflects whether the mouse is over the control.
        Private mouseover As Boolean = False
        ' This color is a private field for the OutlineColor property.
        Private lineColor As Color = Color.White

        ' This color is used to outline the control when the mouse is 
        ' over the control.
        Public Property OutlineColor() As Color
            Get
                Return lineColor
            End Get
            Set(ByVal Value As Color)
                lineColor = Value
            End Set
        End Property

        Public Sub New()
        End Sub 

        ' Sets a value and refreshes the control's display when the 
        ' mouse position enters the area of the control.
        Protected Overrides Sub OnMouseEnter()
            Me.mouseover = True
            Me.Control.Refresh()
        End Sub 

        ' Sets a value and refreshes the control's display when the 
        ' mouse position enters the area of the control.        
        Protected Overrides Sub OnMouseLeave()
            Me.mouseover = False
            Me.Control.Refresh()
        End Sub 

        ' Draws an outline around the control when the mouse is 
        ' over the control.    
        Protected Overrides Sub OnPaintAdornments(ByVal pe As System.Windows.Forms.PaintEventArgs)
            If Me.mouseover Then
                pe.Graphics.DrawRectangle(New Pen(New SolidBrush(Me.lineColor), 6), 0, 0, Me.Control.Size.Width, Me.Control.Size.Height)
            End If
        End Sub 

        ' Adds a property to this designer's control at design time 
        ' that indicates the outline color to use.
        Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
            properties.Add("OutlineColor", TypeDescriptor.CreateProperty(GetType(TestControlDesigner), "OutlineColor", GetType(System.Drawing.Color), Nothing))
        End Sub 
    End Class 

    ' This example control demonstrates the ExampleControlDesigner.
    <DesignerAttribute(GetType(TestControlDesigner))> _
     Public Class TestControl
        Inherits System.Windows.Forms.UserControl
        Private components As System.ComponentModel.Container = Nothing

        Public Sub New()
            components = New System.ComponentModel.Container()
        End Sub 

        Protected Overloads Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub 
    End Class 

End Namespace

[C#] 
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.
    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.
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            properties.Add("OutlineColor", TypeDescriptor.CreateProperty(typeof(ExampleControlDesigner), "OutlineColor", typeof(System.Drawing.Color), null));
        }
    }

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

[C++] 
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Collections;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::Design;

namespace ControlDesignerExample {
public __gc class TestControlDesigner : public System::Windows::Forms::Design::ControlDesigner {
private:
    bool mouseover;
    Color lineColor;

public:
    __property Color get_OutlineColor() {
        return lineColor;
    }

    __property void    set_OutlineColor(Color value) {
        lineColor = value;
    }

    TestControlDesigner() {
        mouseover = false;
        lineColor = Color::White;
    }

protected:
    void OnMouseEnter() {
        this->mouseover = true;
        this->Control->Refresh();
    }    

    void OnMouseLeave() {
        this->mouseover = false;            
        this->Control->Refresh();
    }        

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

protected:
    void PreFilterProperties(System::Collections::IDictionary* properties) {
        properties->Add(S"OutlineColor", TypeDescriptor::CreateProperty(__typeof(TestControlDesigner),
            S"OutlineColor", __typeof(System::Drawing::Color), 0));
    }
};

[DesignerAttribute(__typeof(TestControlDesigner))]
public __gc class TestControl : public System::Windows::Forms::UserControl {        
private:
    System::ComponentModel::Container* components;

public:
    TestControl() {
        components = new System::ComponentModel::Container();
    }

protected:
    void Dispose(Boolean disposing) {
        if (disposing && components)
        {
            components->Dispose();
        }
        UserControl::Dispose(disposing);
    }
};
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms.Design

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

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

See Also

ControlDesigner Members | System.Windows.Forms.Design Namespace | ComponentDesigner | IDesigner | Enhancing Design-Time Support | DesignerAttribute

Show:
© 2014 Microsoft