We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 42,694 times

Contents

Downloads

Related Categories

Introduction to Designers - Something More Useful

divil

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

  • CanBeParentedTo is not called when dragging from t

    Posted by Arthur on 06 Apr 2004

    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 f...