CollectionEditor

vocaris

Newcomer
Joined
Jan 9, 2005
Messages
4
Hello,

I'm new on this page and on VB.Net and want's to create an simple control. But for this I need a property with DataType is collection. In the collection I want to have an object with two properties ('Prop' and 'Value'). I have defined this property, but VB.Net doesn't save my edits in the built in CollectionEditor.

At the Control I have inserted the following code:
Code:
Public ReadOnly Property CustomProperties() As CustomPropertyCollection
   Get
      Return mvcolCustomPropertyCollection
   End Get
End Property

The definition of the CustomPropertyCollection and the object in the collection is:
Code:
Friend Class CustomPropertyConverter
   Inherits System.ComponentModel.TypeConverter

   Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destType As Type) As Boolean
      If destType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor) Then
         Return True
      End If

      Return MyBase.CanConvertTo(context, destType)
   End Function

   Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destType As Type) As Object
      If destType Is GetType(System.ComponentModel.Design.Serialization.InstanceDescriptor) Then
         Dim cat As CustomProperty = DirectCast(value, CustomProperty)

         Dim ci As System.Reflection.ConstructorInfo = GetType(CustomProperty).GetConstructor(System.Type.EmptyTypes)

         Return New System.ComponentModel.Design.Serialization.InstanceDescriptor(ci, Nothing, False)
      End If

      Return MyBase.ConvertTo(context, culture, value, destType)
   End Function
End Class

Public Class CustomProperty
   Dim mvstrProperty As String
   Dim mvstrValue As String

   Public Property Prop() As String
      Get
         Prop = mvstrProperty
      End Get
      Set(ByVal pvstrProp As String)
         mvstrProperty = pvstrProp
      End Set
   End Property

   Public Property Value() As String
      Get
         Value = mvstrValue
      End Get
      Set(ByVal pvstrValue As String)
         mvstrValue = pvstrValue
      End Set
   End Property
End Class

Public Class CustomPropertyCollection
   Inherits CollectionBase

   'Needed to implement the strongly-typed collection
   Default Public Property Item(ByVal Index As Integer) As CustomProperty
      Get
         Return DirectCast(List.Item(Index), CustomProperty)
      End Get
      Set(ByVal Value As CustomProperty)
         List.Item(Index) = Value
      End Set
   End Property

   Public Function Add(ByVal Item As CustomProperty) As Integer
      Return List.Add(Item)
   End Function

   Public Sub Insert(ByVal index As Integer, ByVal Item As CustomProperty)
      If List.Contains(Item) Then
         Throw New InvalidOperationException("Already in list.")
      End If

      List.Insert(index, Item)
   End Sub

   Public Function Contains(ByVal value As CustomProperty) As Boolean
      Return List.Contains(value)
   End Function

   'Redraw the control
   Protected Overrides Sub OnClearComplete()
      MyBase.OnClearComplete()
   End Sub

   'Redraw the control and wire up button's redrawneeded event
   Protected Overrides Sub OnInsertComplete(ByVal index As Integer, ByVal value As Object)
      MyBase.OnInsertComplete(index, value)
   End Sub

   'Redraw the control
   Protected Overrides Sub OnRemoveComplete(ByVal index As Integer, ByVal value As Object)
      MyBase.OnRemoveComplete(index, value)
   End Sub
End Class

The Part with the Converter I have seen in a sample for this page (outlookbartest). I don't know if I need it...

Please help me...
Vocaris
 
And this compiles? I'm more into C#, but when using a setter for a property I always use the 'value' variable supplied by the C# language, so I don't need any 'ByVal var' at the setter code.

Have you tried the sister site of this site yet? www.xtremevbtalk.com ? They're much more into vb .net then this site :)
 
Himo said:
And this compiles? I'm more into C#, but when using a setter for a property I always use the 'value' variable supplied by the C# language, so I don't need any 'ByVal var' at the setter code.
Yes this compiles. You are able to change the 'value' variable in what you want's, e.g. 'pvstrProp'.

Himo said:
Have you tried the sister site of this site yet? www.xtremevbtalk.com ? They're much more into vb .net then this site :)
Thanks for that hint. I'll try it.
 
OK, thanks for the article. Since yesterday I had not read it. But now I have read it, download the demo, copy the files 'ColourButton.vb' and 'ColourButton.resx' into my code and added the 'Buttons' property to my control. The only change I made was to set the 'Browsable'-Flag from the 'Buttons'-Property to True. When I compile my Control and place it in a form the following problems I have:
In the Properties-Window normaly all Collections shows 'Collection' as Value with a small button on the left side. The 'Buttons'-Property have only the small button.
If I add an instance of the ColourButton-Object the 'Modifiers'-Property is 'Assembly' and not 'Friend' like the sample.
The third problem I have is that after editing the collection a message comes what says that the Buttons-Property is Null and that isn't right.

Sorry for my bad english, I hopy you understand me correct. Thanks for helping me.

vocaris
 
Ok, I have found the problem. I have forget the following line in the 'New' Method of the control:

_buttons = New ColourButtonCollection(Me)

Thanks for helping me. The sample above was the key that helped me!

Thanks
Vocaris
 
Back
Top