窗口在Designer中形成usercontrol自定义propertygrid [英] Window forms usercontrol custom propertygrid in the Designer
问题描述
我正在使用VS 2008,VB.Net。我有一个已放在Windows窗体上的usercontrol。当我在设计器并编辑表单并选择了用户控件时,我按f4,我在Propertygrid中看到该用户控件的属性。我需要知道的是如何隐藏所有这些属性并使用我自己的属性创建一个propertygrid。我从工具箱中获得了一个自定义PropertyGrid来运行,但是当我在设计器时,我需要它来显示。
谢谢
-Doug
引用:DougsSoftware - 2小时前
您好,我创建了一个名为UserControl2的类,它实现了ICustomTypeDescriptor,我创建了一个简单的GetProperties函数(如下所示)。它的工作原理除了(Name)属性没有出现在属性网格中。你知道为什么(姓名)不存在吗?我想要一个GetProperties函数的例子,如果你有一个。谢谢!
这是我使用的方法。如果代码内的评论不清楚,请随时提问。只是意识到这对于设计师的房产网格来说是严格的装饰。它不会隐藏通过代码访问的属性。
< ToolboxItem( True )> _
公开 类 LimitedPropertyTextBox
继承 TextBox
实现 ICustomTypeDescriptor
公共 < span class =code-keyword> Sub New ()
' 如果要覆盖基本默认值,则设置属性网格隐藏属性
WordWrap = False
结束 Sub
' 创建PropertyNamesToRemove列表:监视属性名称的字符大小写
' 这些属性不会显示在设计器属性gr上id
' 但是,仍然可以通过代码访问它们。
私有 PropertyNamesToRemove 作为 新列表( 字符串)(新 字符串(){_
AcceptsReturn ,_
AcceptsTab,_
AccessibilityNotifyClients,_
< span class =code-string> AccessibilityObject,_
AccessibleDefaultActionDescription,_
AccessibleDescription,_
AccessibleName,_
AccessibleRole ,_
AllowDrop,_
AutoCompleteCustomSource,_
AutoCompleteMode,_
AutoCompleteSource,_
BindingContext,_
Bounds,_
CanEnableIme,_
CausesValidation,_
CharacterCasing,_
GenerateMember ,_
HideSelection,_
ImeMode,_
行,_
修饰符,_
多行,_
PasswordChar,_
RightToLeft,_
标签,_
Text,_
UseSystemPasswordChar,_
UseWaitCursor,_
WordWrap _
})
' 此方法从
' 提供给设计者propertygrid的PropertyDescriptorCollection
私有 功能 ModifiedPropertyCollection( ByVal pdc As PropertyDescriptorCollecti on) As PropertyDescriptorCollection
Dim ReducedPropertyList As 新 PropertyDescriptorCollection(新 PropertyDescriptor(){})
对于 每个 pd 作为 PropertyDescriptor 在 pdc
如果 不 PropertyNamesToRemove.Contains(pd.Name) 然后
ReducedPropertyList.Add(pd)
结束 如果
下一步
返回 ReducedPropertyList
结束 功能
#RegionICustomTypeDescriptor Members
' 只有前两个函数需要返回非默认值
函数 GetProperties( ByVal attributes() As Attribute) As PropertyDescriptorCollection _
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Dim pdc 作为 PropertyDescriptorCollection = TypeDescriptor.GetProperties( Me ,属性, True )
返回 ModifiedPropertyCollection(pdc)
结束 F. unction
公共 功能 GetProperties() As PropertyDescriptorCollection _
Implements System.ComponentModel.ICustomTypeDescriptor.GetProperties
Dim pdc 作为 PropertyDescriptorCollection = TypeDescriptor.GetProperties( Me , True )
返回 ModifiedPropertyCollection(pdc)
结束 功能
' 对于实现的其余部分,返回默认值
公共 功能 GetConverter()作为 TypeConverter _
Implements System.ComponentModel.ICustomTypeDescriptor.GetConverter
返回 TypeDescriptor.GetConverter (我, True )
结束 功能
公共 功能 GetEvents( ByVal attributes() As Attribute) As EventDescriptorCollection _
Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
返回 TypeDescriptor.GetEvents( Me ,属性, True )
结束 功能
公共 功能 GetEvents()正如 EventDescriptorCollection _
Implements System.ComponentModel.ICustomTypeDescriptor.GetEvents
返回 TypeDescriptor.GetEvents( Me , True )
结束 功能
公开 函数 GetComponentName()作为 字符串 _
Implements System.ComponentModel.ICustomTypeDescriptor.GetComponentName
返回 TypeDescriptor.GetComponentName(我, True )
结束 功能
公开 < span class =code-keyword>功能 GetPropertyOwner( ByVal pd 作为 PropertyDescriptor)< span class =code-keyword>作为 对象 _
实施系统.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner
返回 我
结束 功能
公共 Function GetAttributes() As AttributeCollection _
Implements System.ComponentModel .ICustomTypeDescriptor.GetAttributes
返回 Typ eDescriptor.GetAttributes( Me , True )
结束 功能
公共 功能 GetEditor( ByVal editorBaseType As Type) As 对象 _
Implements System.ComponentModel.ICustomTypeDescriptor.GetEditor
返回 TypeDescriptor.GetEditor( Me ,editorBaseType, True )
结束 功能
公共 功能 GetDefaultProperty()作为 PropertyD escriptor _
Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty
返回 TypeDescriptor.GetDefaultProperty (我, True )
结束 功能
公共 功能 GetDefaultEvent() As EventDescriptor _
Implements System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent
返回 TypeDescriptor.GetDefaultEvent( Me , True )
结束 功能
公开 功能 GetClassName()作为 字符串 _
实现 System.ComponentModel.ICustomTypeDescriptor.GetClassName
返回 TypeDescriptor.GetClassName(我 , True )
结束 功能
#End Region
结束 类
编辑:我在上面说它只影响属性网格,删除的属性将影响任何调用GetProperties的机制,如数据绑定。 />
例如这段代码会失败,因为隐藏了WordWrap属性。
CheckBox1.DataBindings.Add(Checked,LimitedPropertyTextBox1,WordWrap)
谢谢 TnTinMn!您的代码将引导至解决方案。我错误的是 我的陈述...
返回 TypeDescriptor.GetProperties(GetType (UserControl2))
当我使用返回(TypeDescriptor.GetProperties(我) ,属性,真))它工作!
谢谢
-Doug
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
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")
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
这篇关于窗口在Designer中形成usercontrol自定义propertygrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!