Library tutorials & articles

Introduction to Designers

Getting Started

As this article is only meant to be a brief introduction to designers, we won't make anything terribly complicated. Before we can make a designer we need a control to design, so we'll create a simple usercontrol that draws an ellipse in its client area.

To create a custom designer for a control you will either inherit from ControlDesigner or ParentControlDesigner, depending on whether you want users to be able to place child controls in your control. All we need for this article is to inherit from ControlDesigner. You will need to add an assembly reference to System.Design.dll to see these classes. To associate your designer class with your control, you use the DesignerAttribute class:

[VB]
Imports System.Windows.Forms.Design
Imports System.ComponentModel
<Designer(GetType(MyControlDesigner))> _
Public Class UserControl1
    Inherits System.Windows.Forms.UserControl
    Public Sub New()
        MyBase.New()
        SetStyle(ControlStyles.ResizeRedraw, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
    End Sub
    Private Sub UserControl1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawEllipse(Pens.Red, ClientRectangle)
    End Sub
End Class
Friend Class MyControlDesigner
    Inherits ControlDesigner
End Class

[C#]
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace CSharpTest
{
    [Designer(typeof(MyControlDesigner))]
    public class UserControl1 : System.Windows.Forms.UserControl
    {
        public UserControl1()
        {
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            this.Paint += new PaintEventHandler(UserControl1_Paint);
        }
        private void UserControl1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawEllipse(Pens.Red, ClientRectangle);
        }
    }
    internal class MyControlDesigner : System.Windows.Forms.Design.ControlDesigner
    {
    }
}

In the Form designer, you should see the UserControl you just created in the toolbox, and you can drag it on to the form like you would any other control. Although our designer class doesn't do anything yet, the instance on the form is using it.

Comments

  1. 06 Apr 2004 at 05:53

    I'm just going through this article and I've found that the CanBeParentedTo is not called when the control is just dragged from the tool box. It is called only when the control is already sited on a form and then it is dragged into a Panel or GroupBox etc.
    I think it's worth to mention it. I've just spent an hour experimenting
    Arthur

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Introduction to Designers.

Leave a comment

Sign in or Join us (it's free).

Tim Dawson
AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

We'd love to hear what you think! Submit ideas or give us feedback