×

集合类与集合编辑器

Kalet Kalet 发表于2009-03-20 12:00:14 浏览212 评论0

抢沙发发表评论

集合类与集合编辑器

Imports System.ComponentModel
Imports System.ComponentModel.Design集合类与集合编辑器
Imports System.Drawing.Design
<Editor(GetType(BarItemCollectionEditor), GetType(UITypeEditor))> _
Public Class BarItemCollection
    Inherits CollectionBase


    Default Public Property Item(ByVal index As Integer) As BarItem
        Get
            Return CType(List(index), BarItem)
        End Get
        Set(ByVal value As BarItem)
            List(index) = value
        End Set
    End Property



    Public Function Add(ByVal value As BarItem) As Integer
        Return List.Add(value)
    End Function 'Add


    Public Function IndexOf(ByVal value As BarItem) As Integer
        Return List.IndexOf(value)
    End Function 'IndexOf



    Public Sub Insert(ByVal index As Integer, ByVal value As BarItem)
        List.Insert(index, value)
    End Sub 'Insert



    Public Sub Remove(ByVal value As BarItem)
        List.Remove(value)
    End Sub 'Remove



    Public Function Contains(ByVal value As BarItem) As Boolean
        ' If value is not of type Int16, this will return false.
        Return List.Contains(value)
    End Function 'Contains



    Protected Overrides Sub OnInsert(ByVal index As Integer, ByVal value As Object)
        ' Insert additional code to be run only when inserting values.
    End Sub 'OnInsert



    Protected Overrides Sub OnRemove(ByVal index As Integer, ByVal value As Object)
        ' Insert additional code to be run only when removing values.
    End Sub 'OnRemove



    Protected Overrides Sub OnSet(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
        ' Insert additional code to be run only when setting values.
    End Sub 'OnSet



    '......


End Class



Public Class BarItemCollectionEditor
    Inherits CollectionEditor
    Public Sub New(ByVal type As Type)
        MyBase.New(type)
    End Sub


    Public Overloads Overrides Function EditValue( _
    ByVal context As ITypeDescriptorContext, _
    ByVal provider As IServiceProvider, _
    ByVal value As Object) As Object
        Dim returnObject As Object = MyBase.EditValue(context, provider, value)
        'CType(context.Instance, SimpleChart).RebuildChart()
        Return returnObject
    End Function


    Protected Overrides Function CreateInstance(ByVal itemType As Type) As Object
        Dim item As New BarItem("Enter Title Here", 0)
        Return item
    End Function
End Class


 


'控件中代码


Public Class MyControl
    Private _BarItems As New BarItemCollection
    Public Property BarItems() As BarItemCollection
        Get
            Return _BarItems
        End Get
        Set(ByVal value As BarItemCollection)
            _BarItems = value
        End Set
    End Property
End Class

集合类与集合编辑器

 


'将拖放到窗体上后,窗体自动生成的代码


Private Sub InitializeComponent()

        Me.MyControl1 = New CollectionControls.MyControl
        Me.SuspendLayout()
        '
        'MyControl1
        '
New CollectionControls.BarItemCollection.Add(New CollectionControls.BarItem("Item1", 4.55!))
New CollectionControls.BarItemCollection.Add(New CollectionControls.BarItem("Item2", 120.3!))
        Me.MyControl1.Location = New System.Drawing.Point(59, 76)
        Me.MyControl1.Name = "MyControl1"
        Me.MyControl1.Size = New System.Drawing.Size(150, 150)
        Me.MyControl1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.MyControl1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)


    End Sub


Imports System.ComponentModel
Imports System.ComponentModel.Design ' Custom Designers Require this NameSpace
Imports System.ComponentModel.Design.Serialization ' Instance Descriptors
Imports System.Globalization  ' This name space is required for
Imports System.Drawing.Design ' Designers Are in this NameSpace, You Also need to a reference to System.Design.Dll


Public Class Component1


    Private _BarItems As New BarItemCollection
    Public Property BarItems() As BarItemCollection
        Get
            Return _BarItems
        End Get
        Set(ByVal value As BarItemCollection)
            _BarItems = value
        End Set
    End Property


End Class


<Editor(GetType(BarItemCollectionEditor), GetType(UITypeEditor))> _
Public Class BarItemCollection
    Inherits CollectionBase


    Default Public Property Item(ByVal index As Integer) As BarItem
        Get
            Return CType(List(index), BarItem)
        End Get
        Set(ByVal value As BarItem)
            List(index) = value
        End Set
    End Property


End Class


Public Class BarItemCollectionEditor
    Inherits CollectionEditor
    Public Sub New(ByVal type As Type)
        MyBase.New(type)
    End Sub


    Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
        Dim returnObject As Object = MyBase.EditValue(context, provider, value)
        Return returnObject
    End Function


    Protected Overrides Function CreateInstance(ByVal itemType As Type) As Object
        Dim item As New BarItem()
        Return item
    End Function


End Class


<Serializable()> _
Public Class BarItem


    Private _BarImage As Image
    Public Property BarImage() As Image
        Get
            Return _BarImage
        End Get
        Set(ByVal value As Image)
            _BarImage = value
        End Set
    End Property

集合类与集合编辑器

End Class



群贤毕至

访客