Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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:

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

Posted

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? http://www.xtremevbtalk.com ? They're much more into vb .net then this site :)

For questions about VS .net extensibility, please fire at me! :)

For readability, please use the [ CS][/CS ] tags

Posted
And this compiles? I'm more into C#' date=' 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.[/quote']

Yes this compiles. You are able to change the 'value' variable in what you want's, e.g. 'pvstrProp'.

 

Have you tried the sister site of this site yet? http://www.xtremevbtalk.com ? They're much more into vb .net then this site :)

Thanks for that hint. I'll try it.

Posted

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

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...