dimanche 10 mai 2015

Custom control collection - Adding items at design time?

I am trying to make a reusable control similar to an Outlook-style sidebar. I have a CustomPanel. I also have a CustomCollectionControl, that inherits from flow layout panel. At design time I would like to add (x) CustomPanels to my CustomCollectionControl, through the properties window.

When I try to add from the (Collection) list in the properties window, it will show up in the list, but it will not add it to the control that is on the form.

Here is my code so far.

Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class CustomCollectionControl

    Inherits FlowLayoutPanel

    ''' <summary> 
    ''' Required designer variable.
    ''' </summary>
    Private _mComponents As Container = Nothing

    Private _mCustompanels As CustomPanelCollection

    Public Sub New()

        ' This call is required by the Windows.Forms Form Designer.
        InitializeComponent()

        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        _mCustompanels = New CustomPanelCollection(Me)

        Padding = New Padding(0)

    End Sub

#Region "Component Designer generated code"
    ''' <summary> 
    ''' Required method for Designer support - do not modify 
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        _mComponents = New System.ComponentModel.Container()
    End Sub
#End Region

    <EditorBrowsable(EditorBrowsableState.Always)> _
    <Browsable(True)> _
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    <Bindable(True)> _
    Public Property CustomPanels() As CustomPanelCollection
        Get
            Return _mCustompanels
        End Get
        Set(value As CustomPanelCollection)
            _mCustompanels = value
        End Set
    End Property

    Protected Overrides Sub OnResize(e As EventArgs)

        MyBase.OnResize(e)

    End Sub

End Class

Public Class CustomPanelCollection
    Inherits CollectionBase

    Private _mControl As CustomCollectionControl

    Private _mCustomCollectionControl As CustomCollectionControl

    Friend Sub New(control As CustomCollectionControl)

        _mCustomCollectionControl = control

    End Sub

    Default Public ReadOnly Property Item(index As Integer) As CustomPanel
        Get
            Return DirectCast(List(index), CustomPanel)
        End Get
    End Property

    Public Function Contains(cPanel As CustomPanel) As Boolean

        Return List.Contains(cPanel)

    End Function

    Public Function Add(cPanel As CustomPanel) As Integer

        Dim i As Integer

        i = List.Add(cPanel)
        cPanel.Control = _mCustomCollectionControl

        Return i

    End Function

    Public Sub Remove(cPanel As CustomPanel)

        List.Remove(cPanel)
        cPanel.Control = Nothing

    End Sub

End Class

Public Class CustomPanel
    Inherits Panel

    Friend Control As CustomCollectionControl

    Public Sub New()

        ' TODO Set Stuff!
        Height = 100
        BorderStyle = BorderStyle.FixedSingle
        Margin = New Padding(0)
        Padding = New Padding(0)

        Dim cBtn As New Button
        cBtn.Height = 30
        Controls.Add(cBtn)
        cBtn.Dock = DockStyle.Top

    End Sub

End Class

I need to find out when a CustomPanel is added through the properties window during design time, how to update the control with the changes?

Aucun commentaire:

Enregistrer un commentaire