Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,
 
I have been using some great posts already on this site, such as these Creating Collection-Based Controls with Rich Design-Time Support[^] and How to Edit and Persist Collections with CollectionEditor[^] to come up with some code to maintain a custom component within a custom collection within a custom control (see code below).
 
The issue I'm having with my code is that I want the new control to appear with at least 1 item within the collection. The code works fine, but for some reason the item added in the controls constructor neither appears in the designers code, and it doesn't show its correct properties in the collection editor. However, every new component added after with the collection editor does.
 
Can any kind person tell me what I've most likely done wrong?
 
Component
<DesignTimeVisible(False), Serializable(), TypeConverter(GetType(ThumbConverter))> _
Public Class Thumb
    Inherits Component
 
#Region "Fields"
 
    Private _position As Integer
    Private _tag As Object
 
#End Region
 
#Region "Contrustctors"
 
    Sub New()
        MyBase.New()
        _position = 0
        _tag = Nothing
    End Sub
 
#End Region
 
#Region "Properties"
 
    Public Property Position As Integer
        Get
            Return _position
        End Get
        Set(value As Integer)
            _position = value
        End Set
    End Property
 
    <TypeConverter(GetType(StringConverter))> _
    Public Property Tag As Object
        Get
            Return _tag
        End Get
        Set(value As Object)
            _tag = value
        End Set
    End Property
 
#End Region
 
#Region "Methods"
 
#End Region
 
End Class
 
Friend Class ThumbConverter
    Inherits TypeConverter
 
    Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
        If destinationType Is GetType(InstanceDescriptor) Then
            Return True
        End If
        Return MyBase.CanConvertTo(context, destinationType)
    End Function
 
    Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
        If destinationType Is GetType(InstanceDescriptor) Then
            Return New InstanceDescriptor(GetType(Thumb).GetConstructor(New Type() {}), Nothing, False)
        End If
        Return MyBase.ConvertTo(context, culture, value, destinationType)
    End Function
 
End Class
Collection
<Serializable()> _
Public Class ThumbCollection
    Inherits CollectionBase
 
#Region "Fields"
 

#End Region
 
    Sub New()
        MyBase.New()
    End Sub
 
#Region "Constructors"
 
#End Region
 
#Region "Properties"
 
    Default Public ReadOnly Property Item(ByVal index As Integer) As Thumb
        Get
            Return DirectCast(Me.List.Item(index), Thumb)
        End Get
    End Property
 
#End Region
 
#Region "Methods"
 
    Public Function Add(ByVal value As Thumb) As Integer
        Dim index As Integer = Me.List.Add(value)
        Return index
    End Function
 
    Public Sub AddRange(ByVal values() As Thumb)
        For Each value As Thumb In values
            Me.List.Add(value)
        Next
    End Sub
 
    Public Function Contains(ByVal value As Thumb) As Boolean
        Return Me.List.Contains(value)
    End Function
 
    Public Function IndexOf(ByVal value As Thumb) As Integer
        Return Me.List.IndexOf(value)
    End Function
 
    Public Sub Insert(ByVal index As Integer, ByVal value As Thumb)
        Me.List.Insert(index, value)
    End Sub
 
    Public Sub Remove(ByVal value As Thumb)
        Dim index As Integer = Me.IndexOf(value)
        Me.List.RemoveAt(index)
    End Sub
 
#End Region
 
End Class
 
Public Class ThumbCollectionEditor
    Inherits System.ComponentModel.Design.CollectionEditor
 
    Private Types() As System.Type
 
    Sub New(ByVal type As System.Type)
        MyBase.New(type)
        Types = New System.Type() {GetType(Thumb)}
    End Sub
 
    Protected Overrides Function CreateNewItemTypes() As System.Type()
        Return Types
    End Function
 
End Class
Control (code is trimmed down)
Public Class TrackBar
    Inherits Control
 
#Region "Fields"
 
    Private _items As ThumbCollection
 
#End Region
 
#Region "Contrustctors"
 
    Sub New()
        MyBase.New()
        _items = New ThumbCollection()
        Me.Size = New Size(104, 45)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.ResizeRedraw Or ControlStyles.UserPaint, True)
        _items.Add(New Thumb)
    End Sub
 
#End Region
 
#Region "Properties"
 
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Editor(GetType(ThumbCollectionEditor), GetType(UITypeEditor))> _
    Public ReadOnly Property Items As ThumbCollection
        Get
            Return _items
        End Get
    End Property
 
#End Region
 
#Region "Methods"
 
#End Region
 
End Class
 
 
Any help is greatly appreciated.
 
Please note this is my first post and I just code for fun, hopefully I've provided enough information here to answer the question.
 
Regards,
Posted 20-Jan-13 11:35am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,628
1 Tadit Dash 375
2 Manas Bhardwaj 319
3 Sergey Alexandrovich Kryukov 275
4 OriginalGriff 243
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,711
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid