从 UserControl 中删除属性和事件 vb.net

Remove Properties and Events from UserControl vb.net

我正在使用 vb.net 开发我自己的 userControl。我是这个任务的新手。

我想删除默认属性。 在google之后,我发现了几个话题,像这样: Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode

所以,我正在尝试使用它,但对我不起作用。我不知道我遗漏了什么或做错了什么。

    Public Class MyControlDesigner
    Inherits System.Windows.Forms.Design.ControlDesigner

    Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
        MyBase.PreFilterProperties(properties)
        properties.Remove("BackColor")
        properties.Remove("ForeColor")
        properties.Remove("Font")
    End Sub
End Class

    <DesignerAttribute(GetType(MyControlDesigner))> _
Public Class MyUserControl
    ' ...
End Class

为了隐藏覆盖属性,我遵循了这个主题Hiding inherited properties,这对其中一些人来说效果很好。

<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> _
Public Shadows Property AutoScroll() As Boolean
    Get
        Return m_AutoScroll
    End Get
    Set(ByVal value As Boolean)
        m_AutoScroll = value
    End Set
End Property

但是,我还有其他属性不知道如何隐藏或删除。像字体、前景色、边距等...

感谢高级

编辑:一旦我完成我的控制,我不想看到,所有的属性都像图片,只有我想展示我的。

编辑:添加来自@Plutonix 的代码

我无法使用 control/tool/property 编辑器,但您可以尝试使用 TypeConverter。这适用于从 UserControl 继承的控件以隐藏 属性 网格中的属性,但它不会从 VS IDE 属性 编辑器中隐藏它们。

VS IDE 使用反射获取 属性 列表并且显然忽略了 TypeConverter。如果您的工具做类似的事情,这将不起作用 - 同样,我没有测试它的工具,但它很简单,值得一试。

我创建了一个带有一些控件的实际 UserControl。那么:

Imports System.ComponentModel

Public Class YControlConverter
    Inherits TypeConverter

    Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean
        Return True
    End Function

    Public Overrides Function GetProperties(context As ITypeDescriptorContext,
                             value As Object,
                             attributes() As Attribute) As PropertyDescriptorCollection

        Dim propNames() As String = {"backcolor", "forecolor",
                                 "autoscroll", "autoscrollminsize",
                                 "autoscrollmargin", "autoscrolloffset",
                                "autoscrollposition"}

        Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(context.Instance)
        ' collection to store the ones we want:
        Dim myPDCList As New List(Of PropertyDescriptor)

        For Each pd As PropertyDescriptor In pdc
            If propNames.Contains(pd.Name.ToLowerInvariant) = False Then
                myPDCList.Add(pd)
            End If
        Next

        Return New PropertyDescriptorCollection(myPDCList.ToArray())

    End Function
End Class

然后用 TypeConverter:

装饰你的用户控件
<TypeConverter(GetType(YControlConverter))>
Public Class YControl

这基本上通过控件的 PropertyDescriptorCollection 运行,并在返回新集合之前过滤掉不需要的属性。如果有效,只需将名称添加到要隐藏的 propNames 数组即可。在 PropertyGrid 中查看:

如您所见,所有 AutoScroll... 属性以及 BackColor 都被删除。其他人也不见了。如果编辑器将使用您的 TypeConverter 而不是反射,它应该可以工作。

--

如何使用 属性 网格测试您的 TypeConverter。使用带有 属性 网格和按钮的表单,在按钮中单击:

Dim yp As New YControl

PropertyGrid1.SelectedObject = yp

如果道具网格中缺少 AutoScroll... 属性,则您的 TypeConverter 可以正常工作!如果他们仍然显示在其他工具中,它正在使用 VS 之类的反射。