Question CollectionForm Add/Remove Buttons

tim8w

Active member
Joined
Oct 26, 2010
Messages
38
Programming Experience
5-10
I have subclassed the CollectionEditor to gain access to the CollectionForm and want to be able to know when the Add or Remove button has been clicked. Here's my subclass:

VB.NET:
    Imports System.ComponentModel.Design

    Public Class MyCollectionEditor
        Inherits CollectionEditor


        Public Shared bEditingCollection As Boolean = False

        Public Delegate Sub MyFormClosedEventHandler(
            ByVal sender As Object,
            ByVal e As FormClosedEventArgs)

        Public Event MyFormClosed As MyFormClosedEventHandler

        Public Delegate Sub MyFormLoadEventHandler(
            ByVal sender As Object,
            ByVal e As System.EventArgs)

        Public Event MyFormLoad As MyFormLoadEventHandler

        Public Sub New(ByVal type As Type)
            MyBase.New(type)
        End Sub

        Protected Overrides Function CreateCollectionForm() As CollectionForm
            Dim collectionForm As CollectionForm = MyBase.CreateCollectionForm

            AddHandler collectionForm.FormClosed, AddressOf Me.collection_FormClosed
            AddHandler collectionForm.Load, AddressOf Me.collection_FormLoad

            Return collectionForm
        End Function

        Private Sub collection_FormLoad(ByVal sender As Object, ByVal e As System.EventArgs)
            bEditingCollection = True
        End Sub

        Private Sub collection_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
            bEditingCollection = False
        End Sub
    End Class
 
CollectionForm is a MustInherit class and implementations can be different, I can see in the source code that the default implementation (CollectionEditor.CollectionEditorCollectionForm) has these buttons buried down somewhere in TableLayoutPanels, it doesn't seem like one should be able to access them.

Related to this, when looking around the ObservableCollection<T> Class (System.Collections.ObjectModel) | Microsoft Docs often comes up as suggestion since it "provides notifications when items get added, removed".
 
SOLVED: CollectionForm Add/Remove/Up/Down Buttons

Reed Kimble from the MSDN forum showed me how to do it for the Add/Remove and from his code I was able to add the Up/Down buttons as well:

VB.NET:
Protected Overrides Function CreateCollectionForm() As CollectionForm
    Dim collectionForm As CollectionForm = MyBase.CreateCollectionForm

    AddHandler collectionForm.FormClosed, AddressOf Me.collection_FormClosed
    AddHandler collectionForm.Load, AddressOf Me.collection_FormLoad

    Dim panel1 = collectionForm.Controls.Item("overArchingTableLayoutPanel").Controls.Item("addRemoveTableLayoutPanel")
    AddHandler panel1.Controls.Item("addButton").Click, AddressOf AddButton_Click
    AddHandler panel1.Controls.Item("removeButton").Click, AddressOf RemoveButton_Click
    Dim panel2 = collectionForm.Controls.Item("overArchingTableLayoutPanel")
    AddHandler panel2.Controls.Item("upButton").Click, AddressOf UpButton_Click
        AddHandler panel2.Controls.Item("downButton").Click, AddressOf DownButton_Click

    Return collectionForm
End Function

Private Sub AddButton_Click(sender As Object, e As EventArgs)
    MessageBox.Show("Add Button Clicked")
End Sub

Private Sub RemoveButton_Click(sender As Object, e As EventArgs)
    MessageBox.Show("Remove Button Clicked")
End Sub

Private Sub UpButton_Click(sender As Object, e As EventArgs)
    MessageBox.Show("Up Button Clicked")
End Sub

Private Sub DownButton_Click(sender As Object, e As EventArgs)
    MessageBox.Show("Down Button Clicked")
End Sub
 
Back
Top