Search code examples
c#.netvb.netdesign-surface

Custom Designer changes the display of my components on the Design Surface


NB: I put the C# tag to get more visibility to this question, as an answer in C# in fine to me, I will be able to translate it into VB .NET. The problem I face is with the concept in the .NET Framework.

I am currently writing a graphic designer so that users can add my own set of customized components into a design surface, arrange them as they wish and then save it into a text file.

For most part it is working OK, but I have tried to use a custom Designer in order to hide some properties I don't want to show to the users :

Public Class MyDesigner
    Inherits ComponentDesigner

    Public Overrides Sub Initialize(component As IComponent)
        MyBase.Initialize(component)
    End Sub

    'These are the properties I will hide
    ' (real list is way longer)
    Private _hiddenPropertyList As String() = { _
        "AccessibleRole", _
        "AccessibleDescription", _
        "AccessibleName", _
        }

    Protected Overrides Sub PreFilterProperties(properties As IDictionary)
        MyBase.PreFilterProperties(properties)
        For Each PropName As String In _hiddenPropertyList
            If properties.Contains(PropName) Then
                'We hide the properties that are in the list
                properties.Remove(PropName) 
            End If
        Next
    End Sub
End Class

So this is not really complicated. To activate my designer, I must set it as attribute in my component class definition :

<Designer(GetType(MyDesigner))>
Public Class MyLabel
    Inherits Label

    Public Sub New()
        MyBase.New()
    End Sub
End Class

So this is not really complicated either.

The Problem

The problem is, when a new MyLabel is created on the design surface and I have set the DesignerAttribute, it is displayed below the surface, as the picture :

Custom Component wrongly displayed

And here is the result when I remove the DesignAttribute :

Custom Component correctly displayed

Which is what I actually want to display.

My Question

What am I missing here in order to make this work ?


Solution

  • Your label is a Control rather than Component. Thus you should use the System.Windows.Forms.Design.ControlDesigner as a base class that extends the design mode behavior of a Control.