Option Strict On
Option Explicit On
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms.Design
<Designer("CollectionControlDesigner"), _
ProvideProperty("FieldValue", GetType(Control)), _
ProvideProperty("Clm", GetType(Control))> _
Public Class DataExtender
Inherits System.ComponentModel.Component
Implements IExtenderProvider
Private _Columns As DataExtenderCollection
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
_Columns = New DataExtenderCollection(Me)
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
#Region "Extender/Hashtable "
Public Function CanExtend(ByVal extendee As Object) As Boolean _
Implements IExtenderProvider.CanExtend
If TypeOf extendee Is TextBox Then
Return True
Else
Return False
End If
End Function
' this hashtable holds property values for individual controls
Public htProvidedProperties As New Hashtable
Public Function GetAddControl(ByVal ctrl As Control) As DataExtenderProperties
If htProvidedProperties.Contains(ctrl) Then
Return DirectCast(htProvidedProperties(ctrl), DataExtenderProperties)
Else
' add an element to the hashtable
Dim ProvidedProperties As New DataExtenderProperties
htProvidedProperties.Add(ctrl, ProvidedProperties)
Return ProvidedProperties
End If
End Function
#End Region ' Extender/Hastable
'working content
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Columns() As DataExtenderCollection
Get
Return _Columns
End Get
End Property
#Region " Get/Set "
'These need to be excatly the same as the provide properties
Public Class DataExtenderProperties
Public FieldValue As String = String.Empty
Public Clm As DataExtenderCollection
Public List As ListBox
End Class
Public Sub SetList(ByVal ctrl As Control, ByVal Value As ListBox)
Me.GetAddControl(ctrl).List = Value
End Sub
Public Function GetList(ByVal ctrl As Control) As ListBox
If htProvidedProperties.Contains(ctrl) Then
Return DirectCast(Me.htProvidedProperties(ctrl), DataExtenderProperties).List
Else
Return Nothing
End If
End Function
Public Sub SetClm(ByVal ctrl As Control, ByVal Value As DataExtenderCollection)
If htProvidedProperties.Contains(ctrl) Then
Me.GetAddControl(ctrl).Clm = Value
End If
End Sub
Public Function GetClm(ByVal ctrl As Control) As DataExtenderCollection
If htProvidedProperties.Contains(ctrl) Then
Return DirectCast(Me.htProvidedProperties(ctrl), DataExtenderProperties).Clm
End If
End Function
Function GetFieldValue(ByVal ctrl As Control) As String
If htProvidedProperties.Contains(ctrl) Then
Return DirectCast(Me.htProvidedProperties(ctrl), DataExtenderProperties).FieldValue
Else
Return String.Empty
End If
End Function
Sub SetFieldValue(ByVal ctrl As Control, ByVal value As String)
If value = Nothing Then value = String.Empty
Me.GetAddControl(ctrl).FieldValue = value
End Sub
#End Region 'Individual Control Extention
End Class
Friend Class CollectionControlDesigner
Inherits ControlDesigner
Private MyControl As DataExtender
Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
MyBase.Initialize(component)
'Record instance of control we're designing
MyControl = DirectCast(component, DataExtender)
'Hook up events
Dim s As ISelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
Dim c As IComponentChangeService = DirectCast(GetService(GetType(IComponentChangeService)), IComponentChangeService)
AddHandler c.ComponentRemoving, AddressOf OnComponentRemoving
End Sub
Private Sub OnComponentRemoving(ByVal sender As Object, ByVal e As ComponentEventArgs)
Dim c As IComponentChangeService = DirectCast(GetService(GetType(IComponentChangeService)), IComponentChangeService)
Dim button As DataExtenderColumns
Dim h As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
Dim i As Integer
'If the user is removing a button
If TypeOf e.Component Is DataExtenderColumns Then
button = DirectCast(e.Component, DataExtenderColumns)
If MyControl.Columns.Contains(button) Then
c.OnComponentChanging(MyControl, Nothing)
MyControl.Columns.Remove(button)
c.OnComponentChanged(MyControl, Nothing, Nothing, Nothing)
Return
End If
End If
'If the user is removing the control itself
If e.Component Is MyControl Then
For i = MyControl.Columns.Count - 1 To 0 Step -1
button = MyControl.Columns(i)
c.OnComponentChanging(MyControl, Nothing)
MyControl.Columns.Remove(button)
h.DestroyComponent(button)
c.OnComponentChanged(MyControl, Nothing, Nothing, Nothing)
Next
End If
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
Dim s As ISelectionService = DirectCast(GetService(GetType(ISelectionService)), ISelectionService)
Dim c As IComponentChangeService = DirectCast(GetService(GetType(IComponentChangeService)), IComponentChangeService)
'Unhook events
RemoveHandler c.ComponentRemoving, AddressOf OnComponentRemoving
MyBase.Dispose(disposing)
End Sub
Public Overrides ReadOnly Property AssociatedComponents() As System.Collections.ICollection
Get
Return MyControl.Columns
End Get
End Property
Private Sub OnAddButton(ByVal sender As Object, ByVal e As EventArgs)
Dim button As DataExtenderColumns
Dim h As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
Dim dt As DesignerTransaction
Dim c As IComponentChangeService = DirectCast(getservice(GetType(IComponentChangeService)), IComponentChangeService)
'Add a new button to the collection
dt = h.CreateTransaction("Add Button")
button = DirectCast(h.CreateComponent(GetType(DataExtenderColumns)), DataExtenderColumns)
c.OnComponentChanging(MyControl, Nothing)
MyControl.Columns.Add(button)
c.OnComponentChanged(MyControl, Nothing, Nothing, Nothing)
dt.Commit()
End Sub
End Class