2

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

Community
  • 1
  • 1
yaqui
  • 99
  • 1
  • 9
  • What are you trying the remove the properties from: intellisense, user code or the property editor.? Explain what "it doesnt work for me" means. Events cant be removed; in some cases (sub classed controls) you can stop them from firing but they are still there – Ňɏssa Pøngjǣrdenlarp Sep 02 '15 at 12:57
  • What are you trying the remove the properties from: intellisense, user code or the property editor.? I´m trying to remove from my editor, in this case from WinCC (Scada from Siemens) – yaqui Sep 02 '15 at 14:29

1 Answers1

2

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.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • thank you very for your sample. :-) .I tried to implement your code, like the pictures. But sorry , I don´t know what I´m doing wrong. I create a new class YControlConverter, and then apply to my Class Control. But as you can see, my picture from all the properties is not like yours. Still I have all of them. Thanks advanced – yaqui Sep 02 '15 at 19:08
  • I can see some "issues" with your code that may not bear directly on teh situation, but they do not help. A) there is already a Type named `Control` in NET, naming yours the same can cause problems B) Some of those things are just public fields and not properties at all C) What is shown is simple enough that a UserControl seems like massive overkill. How many and what type of UI controls are on that thing? – Ňɏssa Pøngjǣrdenlarp Sep 02 '15 at 20:16
  • Thanks again and after reading your reply, I have created a new UserControl, with name MyControl, problem number A. After that, I have deleted every functions, and variables. And the last thing, I have deleted almost every control, now, I have some labels, checkbox, and textbox, whitout any more code, and I don´t know, but don´t work for me. I know I have been doing something wrong, but really I don´t know what it is. Please, can you send me the project you made to show me how it works? – yaqui Sep 03 '15 at 10:18
  • I strongly urge you to do something like my code does using a property grid: create an instance and view it in the property grid. If the properties are filtered you know your converter works. So if they wont disppear in the editor you are using, it uses Reflection like VS does and you cant do much about that. There is nothing in my test YControl - it is a UC with some controls but no user code at all. If you start with something other than a UserControl as the base (like a panel) there would be fewer props to hide; those you cant, you can still override to ignore changes – Ňɏssa Pøngjǣrdenlarp Sep 03 '15 at 12:39
  • @Plutonix: Your code is working for me, only one question!!! How can I also hide these grayed out properties? – Simos Sigma Apr 04 '18 at 14:01
  • @SimonetosTheGreek There is a way to remove some properties on Custiom Controls but I am not sure about USerControls. It also might matter whether you are talking about the PropertyGrid control versus the VS IDE – Ňɏssa Pøngjǣrdenlarp Apr 04 '18 at 15:10
  • @Plutonix: I am talking about `UserControl` and it's properties in `Properties Window`. If this is what **VS IDE** means (lol). I think the key is this function `Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean`!!! Obviously this loads all supported properties and this is why I see all this grayed out properties and some others which didn't exist before into `UserControl`'s properties. – Simos Sigma Apr 04 '18 at 15:27
  • No, to remove them I think you need a Control Designer and use the PreFilterProperties method. Which properties are you trying to shadow out? – Ňɏssa Pøngjǣrdenlarp Apr 04 '18 at 17:22
  • @Plutonix: Do you mean something like that ( https://stackoverflow.com/a/1486394/6336312 )? – Simos Sigma Apr 05 '18 at 11:28