Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows User-controls VB.NET , +
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 1-May-13 10:03am
Comments
TnTinMn at 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 at 2-May-13 0:06am
   
What's a "custom PropertyGrid"? Is it the class PropertyGrid or not?
—SA
DougsSoftware at 3-May-13 17:57pm
   
"custom propertygrid" means displaying/hiding properties that appear in a propertygrid(obviously).
DougsSoftware at 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 at 3-May-13 18:34pm
   
Yes, I use this technique; that's why I was asking, thanks.
—SA
DougsSoftware at 3-May-13 18:59pm
   
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
<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")
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 
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
 
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 255
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100