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

标签 vb.net user-controls vb.net-2010

我正在使用 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

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

感谢先进

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

image

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

Create a new class

Implements new class

Testing

最佳答案

我无权访问该控件/工具/属性编辑器,但您可以尝试使用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 中查看:

enter image description here

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

--

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

Dim yp As New YControl

PropertyGrid1.SelectedObject = yp

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

关于vb.net - 从 UserControl vb.net 中删除属性和事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353708/

相关文章:

.net - 发布 Excel Interop com 对象的最佳方式

wpf - (WPF) 如何绑定(bind)到用户控件上的 IsMouseOver

asp.net - gplV2 : can i use it for free or not?

c# - 通过用户控件调用页面点击事件

c# - 我们如何在子查询 SQL Server 中分配局部变量

vb.net-2010 - 在 vb.net 中显示文件夹/文件中的图像

mysql - 仅在 vb.net 中查询同一按钮

asp.net - 在后面的代码中使用 SqlDataSource 中的值

c# - 在二维数组中查找元素的位置?

c# - 在多维数组中查找项目的索引