Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,
I am using VS 2008, VB.Net. I have a usercontrol that has been placed on a windows form. And when I am in the designer and editing the form and have the user-control selected, I press f4 and I see the properties in the Propertygrid for that user-control. What I need to find out is how to hide all of those properties and create a propertygrid with my own properties. I got a custom PropertyGrid from the toolbox to run, but I need it to display when I am in the designer.
Thanks
-Doug
Posted
Comments
TnTinMn 1-May-13 16:50pm    
What do you mean by "my own properties"? Do you want to hide the inherited properties from the grid? If, so you can implement System.ComponentModel.ICustomTypeDescriptor on you class and modify the GetProperties functions to return a only those properties you want to show.

If you want an example, just ask.
Sergey Alexandrovich Kryukov 2-May-13 0:06am    
What's a "custom PropertyGrid"? Is it the class PropertyGrid or not?
—SA
[no name] 3-May-13 17:57pm    
"custom propertygrid" means displaying/hiding properties that appear in a propertygrid(obviously).
[no name] 3-May-13 10:15am    
Hi, I created a class named UserControl2 that Implements ICustomTypeDescriptor, and I made a simple GetProperties Function (below). It works except the (Name) property does not appear in the property grid. Do you know why (Name) is not there? I would like an example of a GetProperties Function if you have one. Thanks!


Public Function GetProperties(ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection _
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Return TypeDescriptor.GetProperties(GetType(UserControl2))
End Function
Sergey Alexandrovich Kryukov 3-May-13 18:34pm    
Yes, I use this technique; that's why I was asking, thanks.
—SA

Quote:
DougsSoftware - 2 hrs ago
Hi, I created a class named UserControl2 that Implements ICustomTypeDescriptor, and I made a simple GetProperties Function (below). It works except the (Name) property does not appear in the property grid. Do you know why (Name) is not there? I would like an example of a GetProperties Function if you have one. Thanks!

Here is the method I use. If the in-code comments are not clear, feel free to ask questions. Just realize that this is strictly cosmetic for the designer propertygrid. It does not hide the properties from being accessed via code.
VB
<ToolboxItem(True)> _
Public Class LimitedPropertyTextBox
   Inherits TextBox
   Implements ICustomTypeDescriptor

   Public Sub New()
      ' set your propertygrid hidden properties if you want to override the base default value
      WordWrap = False
   End Sub

   ' create a list of PropertyNamesToRemove:  watchout for the character case of property names
   ' these properties will not be shown on the designer property grid
   ' however, they are still accessible via code.

   Private PropertyNamesToRemove As New List(Of String)(New String() { _
         "AcceptsReturn", _
         "AcceptsTab", _
         "AccessibilityNotifyClients", _
         "AccessibilityObject", _
         "AccessibleDefaultActionDescription", _
         "AccessibleDescription", _
         "AccessibleName", _
         "AccessibleRole", _
         "AllowDrop", _
         "AutoCompleteCustomSource", _
         "AutoCompleteMode", _
         "AutoCompleteSource", _
         "BindingContext", _
         "Bounds", _
         "CanEnableIme", _
         "CausesValidation", _
         "CharacterCasing", _
         "GenerateMember", _
         "HideSelection", _
         "ImeMode", _
         "Lines", _
         "Modifiers", _
         "Multiline", _
         "PasswordChar", _
         "RightToLeft", _
         "Tag", _
         "Text", _
         "UseSystemPasswordChar", _
         "UseWaitCursor", _
         "WordWrap" _
         })

   ' this method removes the properties listed in PropertyNamesToRemove from the 
   ' PropertyDescriptorCollection supplied to the designer propertygrid

   Private Function ModifiedPropertyCollection(ByVal pdc As PropertyDescriptorCollection) As PropertyDescriptorCollection
      Dim ReducedPropertyList As New PropertyDescriptorCollection(New PropertyDescriptor() {})
      For Each pd As PropertyDescriptor In pdc
         If Not PropertyNamesToRemove.Contains(pd.Name) Then
            ReducedPropertyList.Add(pd)
         End If
      Next
      Return ReducedPropertyList
   End Function


#Region "ICustomTypeDescriptor Members"
    ' only these first 2 functions need to return nondefault values

   Function GetProperties(ByVal attributes() As Attribute) As PropertyDescriptorCollection _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
      Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(Me, attributes, True)
      Return ModifiedPropertyCollection(pdc)
   End Function

   Public Function GetProperties() As PropertyDescriptorCollection _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
      Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(Me, True)
      Return ModifiedPropertyCollection(pdc)
   End Function

   ' for the remainder of the implementation, return default values
   Public Function GetConverter() As TypeConverter _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetConverter
      Return TypeDescriptor.GetConverter(Me, True)
   End Function
   Public Function GetEvents(ByVal attributes() As Attribute) As EventDescriptorCollection _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
      Return TypeDescriptor.GetEvents(Me, attributes, True)
   End Function
   Public Function GetEvents() As EventDescriptorCollection _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
      Return TypeDescriptor.GetEvents(Me, True)
   End Function
   Public Function GetComponentName() As String _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetComponentName
      Return TypeDescriptor.GetComponentName(Me, True)
   End Function
   Public Function GetPropertyOwner(ByVal pd As PropertyDescriptor) As Object _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner
      Return Me
   End Function
   Public Function GetAttributes() As AttributeCollection _
   Implements System.ComponentModel.ICustomTypeDescriptor.GetAttributes
      Return TypeDescriptor.GetAttributes(Me, True)
   End Function
   Public Function GetEditor(ByVal editorBaseType As Type) As Object _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetEditor
      Return TypeDescriptor.GetEditor(Me, editorBaseType, True)
   End Function
   Public Function GetDefaultProperty() As PropertyDescriptor _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty
      Return TypeDescriptor.GetDefaultProperty(Me, True)
   End Function
   Public Function GetDefaultEvent() As EventDescriptor _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent
      Return TypeDescriptor.GetDefaultEvent(Me, True)
   End Function
   Public Function GetClassName() As String _
    Implements System.ComponentModel.ICustomTypeDescriptor.GetClassName
      Return TypeDescriptor.GetClassName(Me, True)
   End Function
#End Region
End Class

Edit: I lied above about it only affecting the property grid, the removed properties will impact any mechanism that calls GetProperties such as databinding.

For instance this code would fail, since the "WordWrap" property is hidden.

CheckBox1.DataBindings.Add("Checked", LimitedPropertyTextBox1, "WordWrap")
 
Share this answer
 
v3
SQL
Thank you  TnTinMn! Your code led to the solution. What I was doing wrong was in my statement...
Return TypeDescriptor.GetProperties(GetType(UserControl2))

When I used Return(TypeDescriptor.GetProperties(Me, attributes, True)) it worked!

Thanks
-Doug
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900