yaqui
yaqui

Reputation: 99

Remove Properties and Events from UserControl vb.net

I´m devoloment my own userControl with vb.net. I´m new with this task.

I want to remove default properties. After google, I found several topics, like this: Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode

So, I´m trying to use it, but doesn´t works for me. I don´t know what I missing or doing wrong.

    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

To hide overwrite properties I follow this topic Hiding inherited properties and this works fine, for some of them.

<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

But still, I have other properties that I don´t know how to hide or remove. Like Font, ForeColor, Margin etc...

Thanks advanced

Edit: Once I finish my control, I don´t want to see, all the properties like the picture, Only I want to show mine´s.

image

Edit: Add code from @Plutonix

Create a new class

Implements new class

Testing

Upvotes: 2

Views: 3240

Answers (1)

I do not have access to that control/tool/property editor, but you can try to use a TypeConverter. This works with a control that inherits from UserControl to hide properties from a Property Grid, but it wont hide them from the VS IDE property editor.

The VS IDE uses reflection to get the property list and apparently ignores the TypeConverter. If your tool does something similar, this wont work - again, I dont have the tool to test it, but it is simple and worth a try.

I created an actual UserControl with a few controls on it. Then:

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

Then decorate your usercontrol with the TypeConverter:

<TypeConverter(GetType(YControlConverter))>
Public Class YControl

This basically runs thru the PropertyDescriptorCollection for the control and filters out the unwanted properties before returning the new collection. If it works, just add the names to the propNames array that you want to hide. View in a PropertyGrid:

enter image description here

As you can see, all the AutoScroll... properties are removed as well as BackColor. The others are gone as well. If the editor will use your TypeConverter instead of reflection, it should work.

--

How to test your TypeConverter using a PropertyGrid. Using a form with a property grid and a button, in the button click:

Dim yp As New YControl

PropertyGrid1.SelectedObject = yp

If the AutoScroll... properties are missing from the prop grid, your TypeConverter works! If they still show in the other tool, it is using reflection like VS.

Upvotes: 2

Related Questions