By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,249 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,249 IT Pros & Developers. It's quick & easy.

Implementing a custom control with a typed collection for use at design time.

P: 46
I am having a problem with the Visual Basic property collection editor .

I have created a Custom Type

Expand|Select|Wrap|Line Numbers
  1. Public Class Field
  2.          Private Item As Integer
  3.         Private Name As String = "FieldName"
  4.  
  5.           Public Property FieldName() As String
  6.             Get
  7.                 Return Name
  8.             End Get
  9.             Set(ByVal value As String)
  10.                 Name = value
  11.             End Set
  12.         End Property
  13. End Class
  14.  
And a collection

Expand|Select|Wrap|Line Numbers
  1. Public Class Fields
  2.         Inherits CollectionBase
  3.  
  4.         Default Public Property Item(ByVal Index As Integer) As Field
  5.             Get
  6.                 Return CType(List.Item(Index), Field)
  7.             End Get
  8.             Set(ByVal value As Field)
  9.                 List.Item(Index) = value
  10.             End Set
  11.         End Property
  12.         Public Sub Add(ByVal Item As Field)
  13.             If List.Count = 0 Then
  14.                 List.Add(Item)
  15.             Else
  16.                 For i = 0 To List.Count - 1
  17.                     Dim myField As New Field
  18.                     myField = List(i)
  19.                     If myField.FieldName = Item.FieldName Then
  20.                         Item.FieldName += "1"
  21.                     End If
  22.                 Next
  23.                 List.Add(Item)
  24.             End If
  25.         End Sub
  26.  
  27.     End Class
  28.  
My property declaration uses an instance of the collection:

Expand|Select|Wrap|Line Numbers
  1. Public TableFields As New Fields
  2.  
  3.     Public Property Evarus_Fields() As Fields
  4.         Get
  5.             Return TableFields
  6.         End Get
  7.         Set(ByVal value As Fields)
  8.             TableFields = value
  9.         End Set
  10.     End Property
  11.  

In the designer I am able to access my class and save the items in the collection, but then entries in the designer are incorrect. The created entry actually causes a syntax error on the command ‘New’

Expand|Select|Wrap|Line Numbers
  1. Field1.FieldName = "ColumnA"
  2. New ProjectName.ControlName.Fields.Add(Field1)
  3.  
Modification to the created codes as follows (reference the control instance and the instantiated collection) cures the syntax errors and works as desired.

Expand|Select|Wrap|Line Numbers
  1. Field1.FieldName = "ColumnA"
  2. ControlInstanceName.TableFields.Add(Field1)
  3.  
Any ideas about what my coding error is?
Oct 14 '10 #1

✓ answered by David Gluth

I have resolved my issue.
I needed to provide a “customized” collection editor for my typed class (Field).

Expand|Select|Wrap|Line Numbers
  1. Public Class FieldCollection
  2.         Inherits CollectionEditor
  3.         Public Sub New()
  4.             MyBase.New(GetType(List(Of Field)))
  5.         End Sub
  6.     End Class
  7.  
And then supplied an attribute to the collection property instructing it to use this editor.

Expand|Select|Wrap|Line Numbers
  1. <Editor(GetType(FieldCollection), GetType(UITypeEditor))> _
  2.     <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
  3.     <Description("Fields Collection")> _
  4.     <Category("Behaviour")> _
  5.     Public Property Evarus_Fields() As Fields
  6.         Get
  7.             If TableFields Is Nothing Then
  8.                 TableFields = New Fields
  9.             End If
  10.             Return TableFields
  11.         End Get
  12.         Set(ByVal value As Fields)
  13.             TableFields = value
  14.         End Set
  15.     End Property
  16.  

Share this Question
Share on Google+
1 Reply


P: 46
I have resolved my issue.
I needed to provide a “customized” collection editor for my typed class (Field).

Expand|Select|Wrap|Line Numbers
  1. Public Class FieldCollection
  2.         Inherits CollectionEditor
  3.         Public Sub New()
  4.             MyBase.New(GetType(List(Of Field)))
  5.         End Sub
  6.     End Class
  7.  
And then supplied an attribute to the collection property instructing it to use this editor.

Expand|Select|Wrap|Line Numbers
  1. <Editor(GetType(FieldCollection), GetType(UITypeEditor))> _
  2.     <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
  3.     <Description("Fields Collection")> _
  4.     <Category("Behaviour")> _
  5.     Public Property Evarus_Fields() As Fields
  6.         Get
  7.             If TableFields Is Nothing Then
  8.                 TableFields = New Fields
  9.             End If
  10.             Return TableFields
  11.         End Get
  12.         Set(ByVal value As Fields)
  13.             TableFields = value
  14.         End Set
  15.     End Property
  16.  
Oct 19 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.