在设计时坚持收藏 [英] Persist collection at designtime
问题描述
您好,
我创建了一个我称之为CustomerComponent的组件,该组件只有一个名为Customers的属性,它从我的Customer类接收一个集合。我试图通过UITypeEditor在设计时创建一个表单来编辑这个集合。但是,在设计时添加的值不会反映在.Designer文件中,也就是说,值不会持久存在。请帮我做这个工作。在我的代码下面,最后是我的下载解决方案。
这是我的CustomerComponent类的代码:
公共 类 CustomerComponent
继承组件
公共 属性客户作为 新客户
结束 类
公开 类客户
公共 属性名称作为 字符串
公开 属性年龄 As 整数
结束 类
<编辑器( GetType (Customers.CustomersEditor),的GetType 跨度>(UITypeEditor的))GT;
公开 类客户
私人列表作为 新列表( Of 客户)
公共 功能添加( ByVal 项作为客户)作为客户
List.Add(Item)
返回项目
结束 功能
公共 功能添加( ByVal 名称作为 字符串, ByVal 年龄作为 整数)作为客户
Dim 项目作为 新客户具有 {
.Name =姓名,
。年龄=年龄
}
List.Add(项目)
返回项目
结束 功能
< span class =code-keyword> Public Sub AddRange( ByVal Items()< span class =code-keyword> As Customer)
Dim 项 As 对象
对于 每个项目在项目中s
List.Add(Item)
下一步
结束 < span class =code-keyword> Sub
默认 公众 ReadOnly 属性项目( ByVal 索引作为 整数)作为客户
获取
返回 CType (列表(索引),客户)
结束 获取
结束 属性
公开 < span class =code-keyword>功能 Count() As 整数
返回 List.Count
结束 功能
公开 Sub 清除()
List.Clear()
结束 Sub
公共 覆盖 功能 ToString() As String
返回 没什么
结束 功能
类 CustomersEditor
继承 UITypeEditor
公共 覆盖 功能 GetEditStyle( ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
返回 UITypeEditorEditStyle.Modal
结束 功能
公共 覆盖 函数 EditValue( ByVal context As ITypeDescriptorContext, ByVal 提供程序作为 System.IServiceProvider, ByVal 值作为 对象)< span class =code-keyword>作为 对象
Dim svc < span class =code-keyword> As IWindowsFormsEditorService = TryCast (provider.GetService( GetType (IWindowsFormsEditorService)),IWindowsFormsEditorService)
Dim foo As Customers = TryCast (价值,客户)
如果 svc IsNot Nothing AndAlso foo IsNot Nothing 然后
使用表单 As FormEditor = 新 FormEdi tor()
form.OtherFields = foo
如果 svc.ShowDialog(form)= DialogResult.OK 然后
foo = form.OtherFields
结束 如果
结束 使用
结束 如果
返回值
结束 功能
结束 班级
结束 班级
这里是UITypeEditor中使用的表单代码:
公开 类 FormEditor
公共客户作为 新客户
私人客户作为客户
私有 Sub Button3_Click (发件人作为 对象,e 作为 EventArgs )句柄 Button3.Click
Customer = 新客户使用 {。Name = 未知名称}
PropertyGrid1.SelectedObject = Customer
ListBox1.Items.Add(Cust.Name)
Customers.Add(Customer)
End Sub
私有 Sub FormEditor_Shown(发件人作为 对象,e 正如 EventArgs)句柄 MyBase 。显示
对于 i = 0 Customers.Count - 1
ListBox1.Items.Add(Customers(i).Name)
Next i
结束 Sub
结束 < span class =code-keyword> Class
链接到解决方案
< a href =https://1drv.ms/u/s!AmP2eCNNws8SgqsXekHBgVmFh1zmkA>下载解决方案 [ ^ ]
我有什么试过:
我试图在Customers类中继承CollectionBase类,在这种情况下,添加的值会反映在.Designer文件中但是语法错误。
我修改了你的(对我来说相关)代码以使它工作。我想这会对你有所帮助:Imports System.Windows.Forms.Design
Imports System.ComponentModel
Public Class CustomerComponent
继承组件
< DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
公开 ReadOnly 物业客户作为客户
获取
返回 myCustomers
结束 获取
结束 属性
私有 myCustomers 作为 新客户
结束 类
公开 类客户
公共 属性名称 As String
公开 属性年龄 As 整数
结束 类
公共 类客户
继承 CollectionBase
公共 功能添加( ByVal 项目作为客户)作为客户
List.Add(Item)
返回项目
结束 < span class =code-keyword>功能
公共 功能添加( ByVal 名称作为 字符串, ByVal 年龄作为 整数)作为客户
Dim 项作为 新客户使用 {
.Name =姓名,
。年龄=年龄
}
List.Add(Item)
返回项目
结束 功能
公共 Sub AddRange( ByVal Items() As Customer)
Dim 项目作为 对象
对于 每个项目在项目
List.Add(项目)
下一步
结束 Sub
默认 公开 ReadOnly 属性项目( ByVal 索引作为 整数) As 客户
获取
返回 CType (列表(索引),客户)
结束 获取
结束 属性
' 公共函数计数( )作为整数
' 返回List.Count
' 结束函数
' Public Sub Clear()
' List.Clear()
' End Sub
公共 覆盖 功能 ToString() 作为 字符串
返回 < span class =code-keyword> Nothing
结束 功能
结束 类跨度>
Hello,
Im creating a component that I call CustomerComponent, this component has only one property called Customers that receives a collection from my Customer class. Im trying to create a Form to edit this collection at design time through UITypeEditor. However, the values added at design time do not reflect in the .Designer file, that is, the values do not persist. Please help me make this work. Below my code and in the end my solution to download.
Here is the code for my CustomerComponent class:
Public Class CustomerComponent
Inherits Component
Public Property Customers As New Customers
End Class
Public Class Customer
Public Property Name As String
Public Property Age As Integer
End Class
<Editor(GetType(Customers.CustomersEditor), GetType(UITypeEditor))>
Public Class Customers
Private List As New List(Of Customer)
Public Function Add(ByVal Item As Customer) As Customer
List.Add(Item)
Return Item
End Function
Public Function Add(ByVal Name As String, ByVal Age As Integer) As Customer
Dim Item As New Customer With {
.Name = Name,
.Age = Age
}
List.Add(Item)
Return Item
End Function
Public Sub AddRange(ByVal Items() As Customer)
Dim Item As Object
For Each Item In Items
List.Add(Item)
Next
End Sub
Default Public ReadOnly Property Item(ByVal Index As Integer) As Customer
Get
Return CType(List(Index), Customer)
End Get
End Property
Public Function Count() As Integer
Return List.Count
End Function
Public Sub Clear()
List.Clear()
End Sub
Public Overrides Function ToString() As String
Return Nothing
End Function
Class CustomersEditor
Inherits UITypeEditor
Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
Dim svc As IWindowsFormsEditorService = TryCast(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
Dim foo As Customers = TryCast(value, Customers)
If svc IsNot Nothing AndAlso foo IsNot Nothing Then
Using form As FormEditor = New FormEditor()
form.OtherFields = foo
If svc.ShowDialog(form) = DialogResult.OK Then
foo = form.OtherFields
End If
End Using
End If
Return value
End Function
End Class
End Class
Here the form code used in UITypeEditor:
Public Class FormEditor
Public Customers As New Customers
Private Customer As Customer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Customer = New Customer With {.Name = "Unknown Name"}
PropertyGrid1.SelectedObject = Customer
ListBox1.Items.Add(Cust.Name)
Customers.Add(Customer)
End Sub
Private Sub FormEditor_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
For i = 0 To Customers.Count - 1
ListBox1.Items.Add(Customers(i).Name)
Next i
End Sub
End Class
Link to the solution
Download Solution[^]
What I have tried:
I tried to inherit the CollectionBase class in the Customers class, in which case the added values are reflected in the .Designer file but with a syntax error.
I modified your (for me relevant) Code a little bit to make it work. I think that will help you :Imports System.Windows.Forms.Design Imports System.ComponentModel Public Class CustomerComponent Inherits Component <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> Public ReadOnly Property Customers As Customers Get Return myCustomers End Get End Property Private myCustomers As New Customers End Class Public Class Customer Public Property Name As String Public Property Age As Integer End Class Public Class Customers Inherits CollectionBase Public Function Add(ByVal Item As Customer) As Customer List.Add(Item) Return Item End Function Public Function Add(ByVal Name As String, ByVal Age As Integer) As Customer Dim Item As New Customer With { .Name = Name, .Age = Age } List.Add(Item) Return Item End Function Public Sub AddRange(ByVal Items() As Customer) Dim Item As Object For Each Item In Items List.Add(Item) Next End Sub Default Public ReadOnly Property Item(ByVal Index As Integer) As Customer Get Return CType(List(Index), Customer) End Get End Property 'Public Function Count() As Integer ' Return List.Count 'End Function 'Public Sub Clear() ' List.Clear() 'End Sub Public Overrides Function ToString() As String Return Nothing End Function End Class
这篇关于在设计时坚持收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!