Access A Collection(Of CustomCollection) With A Text String

  • Thread starter Thread starter samialtas
  • Start date Start date
S

samialtas

Guest
Hey there.

I have a custom class called MySampleClasses (Source Link) and I just wanna access it with a text string.

I use this code in order to add a new item.


MyComplexComponent1.MySampleClasses.Add(New CollectionClass("2,9"))
MyComplexComponent1.MySampleClasses.Add(New CollectionClass)



I use this code in order to change an item.

MyComplexComponent1.MySampleClasses(0).Property1 = "1"
MyComplexComponent1.MySampleClasses(0).Property2 = "2"



I plan to have too many custom classes which makes it harder to manage the code smoothly. In order to make it work, I plan to assign tags to ToolStripMenuItems which will determine my multi-purpose form's data source.

Thanks for your help.



Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Reflection

Public Class MyPropertyDescriptor
Inherits PropertyDescriptor

Private ReadOnly O As PropertyDescriptor

Public Sub New(ByVal originalProperty As PropertyDescriptor)
MyBase.New(originalProperty)
O = originalProperty
End Sub

Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Return O.CanResetValue(component)
End Function

Public Overrides Function GetValue(ByVal component As Object) As Object
Return O.GetValue(component)
End Function

Public Overrides Sub ResetValue(ByVal component As Object)
O.ResetValue(component)
End Sub

Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
O.SetValue(component, value)
End Sub

Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Return O.ShouldSerializeValue(component)
End Function

Public Overrides ReadOnly Property Attributes As AttributeCollection
Get
Dim attributes1 = MyBase.Attributes.Cast(Of Attribute)().ToList()
Dim category = attributes1.OfType(Of DescriptionAttribute)().FirstOrDefault()
If category IsNot Nothing AndAlso category.Description = "" Then attributes1.Add(New BrowsableAttribute(False))
Return New AttributeCollection(attributes1.ToArray())
End Get
End Property

Public Overrides ReadOnly Property ComponentType As Type
Get
Return O.ComponentType
End Get
End Property

Public Overrides ReadOnly Property IsReadOnly As Boolean
Get
Return O.IsReadOnly
End Get
End Property

Public Overrides ReadOnly Property PropertyType As Type
Get
Return O.PropertyType
End Get
End Property
End Class

Public Class MyTypeDescriptor
Inherits CustomTypeDescriptor

Private ReadOnly Original As ICustomTypeDescriptor

Public Sub New(ByVal originalDescriptor As ICustomTypeDescriptor)
MyBase.New(originalDescriptor)
Original = originalDescriptor
End Sub

Public Overrides Function GetProperties() As PropertyDescriptorCollection
Return Me.GetProperties(New Attribute() {})
End Function

Public Overrides Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection
Dim properties = MyBase.GetProperties(attributes).Cast(Of PropertyDescriptor)().[Select](Function(p) New MyPropertyDescriptor(p)).ToArray()
Return New PropertyDescriptorCollection(properties)
End Function
End Class

Public Class MyTypeDescriptionProvider
Inherits TypeDescriptionProvider

Public Sub New()
MyBase.New(TypeDescriptor.GetProvider(GetType(Object)))
End Sub

Public Overrides Function GetTypeDescriptor(ByVal type As Type, ByVal o As Object) As ICustomTypeDescriptor
Dim baseDescriptor As ICustomTypeDescriptor = MyBase.GetTypeDescriptor(type, o)
Return New MyTypeDescriptor(baseDescriptor)
End Function
End Class

<TypeDescriptionProvider(GetType(MyTypeDescriptionProvider))>
Public Class CollectionClass
Implements INotifyPropertyChanged

Private _Property2 As String
Private _Property1 As Integer

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub

Public Sub New()
ShowHide()
End Sub

Public Sub New(Txt As String)
ShowHide()
Dim Fields() As String = Txt.Split(Convert.ToChar(","))
Property1 = CInt(Fields(0))
Property2 = Fields(1)
End Sub

Public Sub New(ByVal Property1 As Integer, ByVal Property2 As String)
Me.Property1 = Property1
Me.Property2 = Property2
End Sub

Public Overrides Function ToString() As String
Return "Deneme"
End Function

<[ReadOnly](False)>
<Browsable(True)>
<DescriptionAttribute("")>
<DisplayName("Property1")>
Public Property Property1 As Integer
Get
ShowHide()
Return _Property1
End Get
Set
_Property1 = Value
ShowHide()
NotifyPropertyChanged("Property1")
End Set
End Property

<[ReadOnly](False)>
<Browsable(True)>
<DescriptionAttribute(" ")>
<DisplayName("Property2")>
Public Property Property2 As String
Get
Return _Property2
End Get
Set
_Property2 = Value
End Set
End Property

Public Sub ShowHide()
If MyComplexComponent1.MySampleClasses.Count > 0 Then
If _Property1 = 2 Then
SetBrowsable(MyComplexComponent1.MySampleClasses(ActiveIndex), "Property2", True)
Else
SetBrowsable(MyComplexComponent1.MySampleClasses(ActiveIndex), "Property2", False)
End If
If _Property1 = 1 Then
SetReadOnly(MyComplexComponent1.MySampleClasses(ActiveIndex), "Property2", False)
Else
SetReadOnly(MyComplexComponent1.MySampleClasses(ActiveIndex), "Property2", True)
End If
End If
End Sub

End Class

Public Class MainClass
Inherits Component

Public Sub New()
MySampleClasses = New Collection(Of CollectionClass)()
End Sub

<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property MySampleClasses As Collection(Of CollectionClass)

End Class

Continue reading...
 
Back
Top