Library tutorials & articles

Introduction to Designers

Something More Useful

Now let's try preventing the user from resizing the control vertically, like the TextBox does. To do this you need to override the SelectionRules property of the ControlDesigner class, and return all flags for sizing and moving except the Top and Bottom ones:

[VB]
Friend Class MyControlDesigner
    Inherits ControlDesigner
    Public Overrides ReadOnly Property SelectionRules() As _
    System.Windows.Forms.Design.SelectionRules
        Get
            Return SelectionRules.LeftSizeable Or SelectionRules.RightSizeable Or _
            SelectionRules.Moveable Or SelectionRules.Visible
        End Get
    End Property
End Class

[C#]
public override System.Windows.Forms.Design.SelectionRules SelectionRules
{
    get
    {
        return System.Windows.Forms.Design.SelectionRules.LeftSizeable |
            System.Windows.Forms.Design.SelectionRules.RightSizeable |
            System.Windows.Forms.Design.SelectionRules.Visible |
            System.Windows.Forms.Design.SelectionRules.Moveable;
    }
}

Now, if you create an instance of your control on the form, you'll notice that the designer has disabled all sizing grips that would allow you to size it vertically.

Next, we'll expand our designer to illustrate another cool feature; choosing what controls our control can be parented to. We will make it so that our control cannot be parented to Panel controls, but everything else will be ok. This may seem like a fruitless exercise, but it demonstrates a technique not uncommon when writing designers. All we have to do is override the CanBeParentedTo function and see if the potential parent designer is hosting a control of type Panel:

[VB]
Public Overrides Function CanBeParentedTo(ByVal parentDesigner As _
System.ComponentModel.Design.IDesigner) As Boolean
    If TypeOf parentDesigner.Component Is Panel Then
        Return False
    Else
        Return True
    End If
End Function

[C#]
public override bool CanBeParentedTo(System.ComponentModel.Design.IDesigner
parentDesigner)
{
    if (parentDesigner.Component is Panel)
        return false;
    else
        return true;
}

You will now find that your control can be dragged in to most parent controls, like the GroupBox for example, but not Panel controls.

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.

Want to stay in touch with what's going on? Follow us on twitter!