|
Namespace Controls.Form
Public Class ManageArrayItems
Implements IManageItems
Sub New(items As Object)
If items Is Nothing Then Throw New NullReferenceException
If Not items.GetType.IsArray Then Throw New InvalidCastException("Must be an array")
Me.Items = items
End Sub
Public Property NewItem As CreateNewItem Implements IManageItems.NewItem
Public Property Items As Object Implements IManageItems.Items
Public Property CanAdd As Boolean = True Implements IManageItems.CanAdd
Public Property CanDelete As Boolean = True Implements IManageItems.CanDelete
Public Function Add() As Object Implements IManageItems.Add
If Not CanAdd Then Return Nothing
Dim newObj = NewItem
If newObj IsNot Nothing AndAlso newObj.GetType.GetMethod("Invoke") IsNot Nothing Then
newObj = newObj.Invoke()
End If
If newObj Is Nothing Then
Dim newType = Items.GetType.GetElementType()
newObj = Activator.CreateInstance(newType)
End If
ReDim Preserve Items(Items.Length + 1)
Items(Items.Length - 1) = newObj
Return newObj
End Function
Public Sub Delete(index As Integer) Implements IManageItems.Delete
If Not CanDelete Then Return
For i = index To Items.Length - 2
Items(i) = Items(i + 1)
Next
ReDim Preserve Items(Items.Length - 1)
End Sub
Public Function View() As ComponentModel.ICollectionView Implements IManageItems.View
Return CollectionViewSource.GetDefaultView(Items)
End Function
End Class
End Namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I enjoy my wife, living in the woods, my 7 dogs, and learning new things. I like to play with UI stuff and model based coding.