如何从智能标签打开收集器 [英] How to open collectioneditor from smart tag
问题描述
我在动词中打开CollectionEditor时遇到问题。从属性窗口编辑时它工作正常但是当我从动词中打开CollectionEditor时,它在添加新项目后给了我一个非组件项目。
这是组件项目。
CC1.PNG
这是我从动词添加项目后得到的结果。
CC2.PNG
如果你看到,在添加项目后,数据和设计部分缺失或新项目不是组件。
我尝试过:
以下是我的代码。我错过了什么吗?提前谢谢。
Imports System.Drawing.Design
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms.Design
Friend Class NavCollectionControlDesigner
Inherits ControlDesigner
Private MyControl As mSideBarPanelNav
Public Overrides Sub Initialize(组件为IComponent)
MyBase.Initialize(Component)
'记录我们正在设计的控件实例
MyControl = DirectCast(Component,mSideBarPanelNav)
End Sub
受保护的覆盖Sub Dispose(disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
Public Overrides ReadOnly属性AssociatedComponents()As System。 Collections.ICollection
获取
返回MyControl.Items
结束获取
结束属性
公共覆盖ReadOnly Property Verbs()As System.Compon entModel.Design.DesignerVerbCollection
获取
Dim v As New DesignerVerbCollection()
'动词添加项目
v.Add(新DesignerVerb(& Edit Items) ,New EventHandler(AddressOf OnAddButton)))
返回v
结束获取
结束属性
私有子OnAddButton(发送者作为对象,e As System.EventArgs)
Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(MyControl)(Items)
Dim editor As UITypeEditor = DirectCast(pd.GetEditor(GetType(UITypeEditor)),UITypeEditor)
Dim serviceProvider As New RuntimeServiceProvider()
editor.EditValue(serviceProvider,serviceProvider,MyControl.Items)
End Sub
End Class
公共类RuntimeServiceProvider
实现IServiceProvider
实现ITypeDescriptorContext
私有函数IServiceProvider_GetService(serviceT ype As Type)As Object Implements IServiceProvider.GetService
if serviceType = GetType(IWindowsFormsEditorService)Then
返回新的WindowsFormsEditorService()
结束如果
返回没有
结束函数
私有类WindowsFormsEditorService
实现IWindowsFormsEditorService
Public Sub DropDownControl(控件作为控件)实现IWindowsFormsEditorService.DropDownControl
MsgBox( control.Name)
End Sub
Public Sub CloseDropDown()实现IWindowsFormsEditorService.CloseDropDown
End Sub
公共函数ShowDialog(对话框格式)As System.Windows.Forms.DialogResult实现IWindowsFormsEditorService.ShowDialog
dialog.Font =新字体(Segoe UI,9,FontStyle.Regular)
返回dialog.ShowDialog()
结束函数
结束等级
Public Sub OnComponentChanged()实现ITypeDescriptorContext.OnComponentChanged
结束Sub
公共函数OnComponentChanging()As Boolean实现ITypeDescriptorContext.OnComponentChanging
返回( (不是CType(Me,ITypeDescriptorContext).PropertyDescriptor Is Nothing)AndAlso Not CType(Me,ITypeDescriptorContext).PropertyDescriptor.IsReadOnly)
'返回True
'如果保持更改,则为true,否则为false
End函数
Public ReadOnly Property Container()作为IContainer实现ITypeDescriptorContext.Container
获取
返回Nothing
结束获取
结束房产
Public ReadOnly Property Instance()As Object Implements ITypeDescriptorContext.Instance
Get
Return Nothing
End Get
End Property
Public ReadOnly Property PropertyDescriptor( )作为PropertyDescriptor实现ITypeDescriptorContext.PropertyDescriptor
获取
返回Nothing
结束获取
结束房产
结束类
公共类项目
继承组件
'与问题无关的代码
结束类
< Designer(GetType(NavCollectionControlDesigner))>
公共类mSideBarPanelNav
继承控制
私有_Items为新ItemsCollection()
< Category(Item Collections)>
< DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
< Editor(GetType(ItemCollectionEditor),GetType(System.Drawing.Design.UITypeEditor))>
Public ReadOnly Property Items()As ItemsCollection
Get
Return _Items
End Get
End Property
'与问题无关的代码
结束类
如果您的ItemsCollection看起来像这样,它应该像你想要的那样工作:
公共 类 ItemCollection
继承 CollectionBase
公共 Sub 添加( ByVal item As Item)
List.Add(item)
结束 Sub
公开 属性项目( ByVal index 作为 整数)作为项
获取
返回列表(索引)
结束 获取
设置( ByVal value As 项目)
列表(索引)=值
结束 < span class =code-keyword>设置
结束 物业
受保护的 覆盖 Sub OnInsertComplete( ByVal index 作为 整数, ByVal 值作为 对象)
Dim item As Item = value
MyBase .OnInsertComplete(index,item)
结束 Sub
受保护 覆盖 Sub OnRemoveComplete( ByVal index 作为 整数, ByVal value As Object )
Dim item As Item = value
MyBase .OnRemoveComplete(index,item)
结束 Sub
结束 Class
...但您也可以使用SmartTag以不同的方式访问它...设计师......
以不同的方式向您展示(我替换并修改了您的Designer-Class):
公共 类 NavCollectionControlDesigner
继承 ControlDesigner
私人 MyControl 作为 mSideBarPanelNav
公共 覆盖 Sub Initialize(Component As IComponent)
MyBase .Initialize(Component)
' 记录我们正在设计的控件实例
MyControl = DirectCast (Component,mSideBarPanelNav)
结束 Sub
受保护的 覆盖 Sub Dispose(disposing) 作为 布尔)
MyBase .Dispose(disposing)
End Sub
私有 _ActionLists 作为 DesignerActionListCollection
公众 覆盖 ReadOnly 属性 ActionLists As System.ComponentModel.Design.DesignerActionListCollection
获取
如果 _ActionLists Nothing 然后
_ActionLists = 新 DesignerActionListCollection()
_ActionLists.Add(新 NavCollectionControlSmartTags( Me ))
结束 如果
返回 _ActionLists
结束 获取
结束 属性
结束 类
公共 类 NavCollectionControlSmartTags
继承 DesignerActionList
私有 _Designer 作为 NavCollectionControlDesigner
私有 _ActiveControl 作为 mSideBarPanelNav
< span class =code-keyword> Private _DesignerActionUIService As DesignerActionUIService
Public Sub 新( ByVal designer As NavCollectionControlDesigner)
MyBase 。 New (designer.Component)
_Designer = design er
MyBase .AutoShow = False ' True =如果新安装Control,则直接打开SmartTag
_ActiveControl = TryCast (designer.Component,mSideBarPanelNav)
_DesignerActionUIService = DirectCast ( Me .GetService( GetType (DesignerActionUIService)),DesignerActionUIService)
结束 Sub
公共 覆盖 函数 GetSortedActionItems()作为 DesignerActionItemCollection
Dim Itemlist AsNew DesignerActionItemCollection()
如果 _ActiveControl IsNot Nothing 然后
Itemlist.Add(新 DesignerActionHeaderItem( 解释文本, 拥有))
Itemlist.Add(新 DesignerActionPropertyItem( Items_InActionList, < span class =code-string>声明的项目, 查看,PropertyHelper .GetDescription(_ActiveControl, Items)))
Itemlist。添加(新 DesignerActionPropertyItem( BackColor_InActionList , BackColor, Colors,PropertyHelper.GetDescription(_ActiveControl, BackColor)))
结束 如果
返回项目列表
结束 功能
ReadOnly 属性 Items_InActionList()作为 ItemCollection
获取
返回 _ActiveControl.Items
结束 获取
结束 属性
公共 属性 BackColor_InActionList() As 颜色
获取
返回 _ActiveControl.BackColor
结束 获取
设置( ByVal 值 As 颜色)
_ActiveControl.BackColor = value
结束 设置
结束 属性
结束 类跨度>
I have a problem opening CollectionEditor from verb. It works fine when editing from property window but when I open the CollectionEditor from verbs, it gives me a non component item after adding a new item.
this is the component item.
CC1.PNG
this is what I get after adding an item from verbs.
CC2.PNG
If you can see, after adding an item, Data and Design sections are missing or the new item is not a component.
What I have tried:
below is my code. Am I missing something? thanks in advance.
Imports System.Drawing.Design Imports System.ComponentModel Imports System.ComponentModel.Design Imports System.Windows.Forms.Design Friend Class NavCollectionControlDesigner Inherits ControlDesigner Private MyControl As mSideBarPanelNav Public Overrides Sub Initialize(Component As IComponent) MyBase.Initialize(Component) ' Record instance of control we're designing MyControl = DirectCast(Component, mSideBarPanelNav) End Sub Protected Overrides Sub Dispose(disposing As Boolean) MyBase.Dispose(disposing) End Sub Public Overrides ReadOnly Property AssociatedComponents() As System.Collections.ICollection Get Return MyControl.Items End Get End Property Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection Get Dim v As New DesignerVerbCollection() ' Verb to add Items v.Add(New DesignerVerb("&Edit Items", New EventHandler(AddressOf OnAddButton))) Return v End Get End Property Private Sub OnAddButton(sender As Object, e As System.EventArgs) Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(MyControl)("Items") Dim editor As UITypeEditor = DirectCast(pd.GetEditor(GetType(UITypeEditor)), UITypeEditor) Dim serviceProvider As New RuntimeServiceProvider() editor.EditValue(serviceProvider, serviceProvider, MyControl.Items) End Sub End Class Public Class RuntimeServiceProvider Implements IServiceProvider Implements ITypeDescriptorContext Private Function IServiceProvider_GetService(serviceType As Type) As Object Implements IServiceProvider.GetService If serviceType = GetType(IWindowsFormsEditorService) Then Return New WindowsFormsEditorService() End If Return Nothing End Function Private Class WindowsFormsEditorService Implements IWindowsFormsEditorService Public Sub DropDownControl(control As Control) Implements IWindowsFormsEditorService.DropDownControl MsgBox(control.Name) End Sub Public Sub CloseDropDown() Implements IWindowsFormsEditorService.CloseDropDown End Sub Public Function ShowDialog(dialog As Form) As System.Windows.Forms.DialogResult Implements IWindowsFormsEditorService.ShowDialog dialog.Font = New Font("Segoe UI", 9, FontStyle.Regular) Return dialog.ShowDialog() End Function End Class Public Sub OnComponentChanged() Implements ITypeDescriptorContext.OnComponentChanged End Sub Public Function OnComponentChanging() As Boolean Implements ITypeDescriptorContext.OnComponentChanging Return ((Not CType(Me, ITypeDescriptorContext).PropertyDescriptor Is Nothing) AndAlso Not CType(Me, ITypeDescriptorContext).PropertyDescriptor.IsReadOnly) 'Return True ' true to keep changes, otherwise false End Function Public ReadOnly Property Container() As IContainer Implements ITypeDescriptorContext.Container Get Return Nothing End Get End Property Public ReadOnly Property Instance() As Object Implements ITypeDescriptorContext.Instance Get Return Nothing End Get End Property Public ReadOnly Property PropertyDescriptor() As PropertyDescriptor Implements ITypeDescriptorContext.PropertyDescriptor Get Return Nothing End Get End Property End Class Public Class Item Inherits Component 'codes not related to the issue End Class <Designer(GetType(NavCollectionControlDesigner))> Public Class mSideBarPanelNav Inherits Control Private _Items As New ItemsCollection() <Category("Item Collections")> <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> <Editor(GetType(ItemCollectionEditor), GetType(System.Drawing.Design.UITypeEditor))> Public ReadOnly Property Items() As ItemsCollection Get Return _Items End Get End Property 'codes not related to the issue End Class
If your ItemsCollection Looks like this it should work like you want :
Public Class ItemCollection Inherits CollectionBase Public Sub Add(ByVal item As Item) List.Add(item) End Sub Public Property Item(ByVal index As Integer) As Item Get Return List(index) End Get Set(ByVal value As Item) List(index) = value End Set End Property Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object) Dim item As Item = value MyBase.OnInsertComplete(index, item) End Sub Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object) Dim item As Item = value MyBase.OnRemoveComplete(index, item) End Sub End Class
... but you could also access it in a different way with the SmartTag-Designer ...
to show you a different way (I replaced and modified your Designer-Class) :
Public Class NavCollectionControlDesigner Inherits ControlDesigner Private MyControl As mSideBarPanelNav Public Overrides Sub Initialize(Component As IComponent) MyBase.Initialize(Component) ' Record instance of control we're designing MyControl = DirectCast(Component, mSideBarPanelNav) End Sub Protected Overrides Sub Dispose(disposing As Boolean) MyBase.Dispose(disposing) End Sub Private _ActionLists As DesignerActionListCollection Public Overrides ReadOnly Property ActionLists As System.ComponentModel.Design.DesignerActionListCollection Get If _ActionLists Is Nothing Then _ActionLists = New DesignerActionListCollection() _ActionLists.Add(New NavCollectionControlSmartTags(Me)) End If Return _ActionLists End Get End Property End Class Public Class NavCollectionControlSmartTags Inherits DesignerActionList Private _Designer As NavCollectionControlDesigner Private _ActiveControl As mSideBarPanelNav Private _DesignerActionUIService As DesignerActionUIService Public Sub New(ByVal designer As NavCollectionControlDesigner) MyBase.New(designer.Component) _Designer = designer MyBase.AutoShow = False 'True= the SmartTag is directly opened if the Control is newly placed _ActiveControl = TryCast(designer.Component, mSideBarPanelNav) _DesignerActionUIService = DirectCast(Me.GetService(GetType(DesignerActionUIService)), DesignerActionUIService) End Sub Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection Dim Itemlist As New DesignerActionItemCollection() If _ActiveControl IsNot Nothing Then Itemlist.Add(New DesignerActionHeaderItem("explaining Text", "Own")) Itemlist.Add(New DesignerActionPropertyItem("Items_InActionList", "declared Items", "View", PropertyHelper.GetDescription(_ActiveControl, "Items"))) Itemlist.Add(New DesignerActionPropertyItem("BackColor_InActionList", "BackColor", "Colors", PropertyHelper.GetDescription(_ActiveControl, "BackColor"))) End If Return Itemlist End Function ReadOnly Property Items_InActionList() As ItemCollection Get Return _ActiveControl.Items End Get End Property Public Property BackColor_InActionList() As Color Get Return _ActiveControl.BackColor End Get Set(ByVal value As Color) _ActiveControl.BackColor = value End Set End Property End Class
这篇关于如何从智能标签打开收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!