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()
WordWrap = False
End Sub
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" _
})
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"
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
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")