如何设计像ListView.Items这样的属性和生成的代码

farrio 2005-06-28 04:13:23
我自己做的一个控件里面有一个集合类,通过Property在设计时显示到PropertyGrid里面,点击右面的"..."按钮进行设定。现在的问题是,我希望设计器在代码页里面生成的代码,要像ListView.Items这样的属性一样,写成Items.AddRange(New ....)这种自定义的代码。
目前我已经可以正确将集合内容从设计器里面返回,但是不能把它序列化为我要的形式。
不知道如何能够实现。

自定义的控件

Public Class MyComponent
Inherits ComponentModel.Component

Private m_cl As CompCollection

<ComponentModel.Editor(GetType(MyValueEditor), GetType(Drawing.Design.UITypeEditor)), _
ComponentModel.TypeConverter(GetType(MyValueTypeConvertor)), _
ComponentModel.DesignerSerializationVisibility(ComponentModel.DesignerSerializationVisibility.Content)> _
Public Property Comps() As CompCollection
Get
Return m_cl
End Get
Set(ByVal Value As CompCollection)
m_cl = Value
Dim c As ComponentModel.Component
For Each c In m_cl.Items
MsgBox(c.Site.Name)
Next
End Set
End Property

Public Sub New()
m_cl = New CompCollection
End Sub

End Class

集合类

Public Class CompCollection
Inherits Object

Private m_list As ArrayList

Public ReadOnly Property Items() As ComponentModel.Component()
Get
Return m_list.ToArray(GetType(ComponentModel.Component))
End Get
End Property

Public Sub New()
m_list = New ArrayList
End Sub

Public Sub Add(ByVal c As ComponentModel.Component)
m_list.Add(c)
End Sub

Public Sub Remove(ByVal c As ComponentModel.Component)
m_list.Remove(c)
End Sub

Public Sub AddRange(ByVal cs() As ComponentModel.Component)
m_list.AddRange(cs)
End Sub

Public Overrides Function ToString() As String
Return "(Collection)"
End Function

End Class

TypeConvertor类

Public Class MyValueTypeConvertor
Inherits ComponentModel.TypeConverter

Public Overloads Overrides Function GetPropertiesSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function

Public Overloads Overrides Function GetProperties(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal value As Object, ByVal attributes() As System.Attribute) As System.ComponentModel.PropertyDescriptorCollection
Return ComponentModel.TypeDescriptor.GetProperties(GetType(CompCollection))
End Function

End Class

还有自定义的UIEditor就不贴出来了,因为和问题本身没什么关系。
...全文
129 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Snrmnm_sx 2005-06-29
  • 打赏
  • 举报
回复
Private Sub CreateMyListView()
' Create a new ListView control.
Dim listView1 As New ListView()
listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

' Set the view to show details.
listView1.View = View.Details
' Allow the user to edit item text.
listView1.LabelEdit = True
' Allow the user to rearrange columns.
listView1.AllowColumnReorder = True
' Display check boxes.
listView1.CheckBoxes = True
' Select the item and subitems when selection is made.
listView1.FullRowSelect = True
' Display grid lines.
listView1.GridLines = True
' Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending

' Create three items and three sets of subitems for each item.
Dim item1 As New ListViewItem("item1", 0)
' Place a check mark next to the item.
item1.Checked = True
item1.SubItems.Add("1")
item1.SubItems.Add("2")
item1.SubItems.Add("3")
Dim item2 As New ListViewItem("item2", 1)
item2.SubItems.Add("4")
item2.SubItems.Add("5")
item2.SubItems.Add("6")
Dim item3 As New ListViewItem("item3", 0)
' Place a check mark next to the item.
item3.Checked = True
item3.SubItems.Add("7")
item3.SubItems.Add("8")
item3.SubItems.Add("9")

' Create columns for the items and subitems.
listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

'Add the items to the ListView.
listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

' Create two ImageList objects.
Dim imageListSmall As New ImageList()
Dim imageListLarge As New ImageList()

' Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

'Assign the ImageList objects to the ListView.
listView1.LargeImageList = imageListLarge
listView1.SmallImageList = imageListSmall

' Add the ListView to the control collection.
Me.Controls.Add(listView1)
End Sub 'CreateMyListView
Snrmnm_sx 2005-06-29
  • 打赏
  • 举报
回复
http://search.microsoft.com/search/results.aspx?st=b&na=88&View=zh-cn&qu=LISTVIEW
farrio 2005-06-29
  • 打赏
  • 举报
回复
难道没有人会么?
ccghghgj 2005-06-28
  • 打赏
  • 举报
回复
关注一下。


================================================================
此帖通过csdn小助手回复。
CSDN小助手是使用vb.net编写的CSDN论坛脱机“外挂”,她能够在
脱离IE的情况下使用Csdn论坛。程序只加载最核心的数据,所以显示更
快,产生的流量更小。

下载地址:http://qqwwee.com/csdn.rar
================================================================

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧