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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”