collectbase with a pagelist of Panels

a_jam_sandwich

Junior Contributor
Joined
Dec 10, 2002
Messages
367
Location
Uk
I have a collectbase with a pagelist of Panels alls great can add remove etc but

How do I make the panels visible within the Usercontrol?

Andy
 
You will need to synchronize the panel collection with the Control
collection on the UserControl (that is, when you add to the panel
collection, add to the control collection, when you remove on the
panel collection, remove from the control collection, etc).

You can do this by doing Me.Controls.Add(thePanelObject). Then
you will need to manually make each panel visible/invisible when you
want to see them.
 
I dont quite get what you mean the Panels will be container as in normal heres the source

Visual Basic:
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization

Public Class Pagelist
    Inherits System.Windows.Forms.UserControl

    Private _pagecollection As New PageCollection()
    Private _selectedPage As Panel

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property Pages() As PageCollection
        Get
            Return _pagecollection
            Invalidate()
        End Get
    End Property

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        _pagecollection = New PageCollection()
        'Add any initialization after the InitializeComponent() call

    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

End Class

Public Class PageCollection
    Inherits CollectionBase

    Default Public Property Item(ByVal Index As Integer) As Panel
        Get
            Return DirectCast(list.Item(Index), Panel)
        End Get
        Set(ByVal Value As Panel)
            list.Item(Index) = Value
        End Set
    End Property

    Public Function Add(ByVal Item As Panel) As Integer
        Return List.Add(Item)
    End Function

End Class

'Public Class PageConverter
'    Inherits TypeConverter


'    Public Overloads Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
'        If destinationType Is GetType(InstanceDescriptor) Then
'            Return True
'        End If
'        Return MyBase.CanConvertTo(context, destinationType)
'    End Function

'    Public Overloads Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
'        If destinationType Is GetType(InstanceDescriptor) Then
'            Dim cat As Panel = DirectCast(value, Panel)
'            Dim cl As System.Reflection.ConstructorInfo = GetType(Panel).GetConstructor(System.Type.EmptyTypes)
'            Return New InstanceDescriptor(cl, Nothing, False)
'        End If
'        Return MyBase.ConvertTo(context, culture, value, destinationType)
'    End Function

'End Class

Cheers

Andy
 
Well, the way I would do it is to store a "ParentUserControl" property
within the PanelCollection. Then, when you create the PanelCollection,
set the ParentUserControl = the UserControl you're using (Me).
Then, in the Add method of the PanelCollection, use

_parentUserControl.Controls.Add(myPanelObject)

_parentUserControl being the member variable containing the reference
to the parent UserControl.
 
OK, in your PageCollection class, add a member variable:
Visual Basic:
Private _parentUserControl As UserControl
and change the constructor of the PageCollection as well:
Visual Basic:
Public Sub New(parent As UserControl)
  _parentUserControl = parent
End Sub
This will store the control that the class is going to be used with, thus
exposing its Control collection. Now, change the Add method:
Visual Basic:
Public Function Add(ByVal Item As Panel) As Integer
  _parentUserControl.Controls.Add(Item)
  Return List.Add(Item)
End Function
That will add the panel to the UserControl. Finally, you need to
change the constructor of the PageList class:
Visual Basic:
Public Sub New()
  MyBase.New()
  'This call is required by the Windows Form Designer.
  InitializeComponent()
  SetStyle(ControlStyles.AllPaintingInWmPaint, True)
  SetStyle(ControlStyles.DoubleBuffer, True)
  _pagecollection = New PageCollection(Me) '<-- I added the [b]Me[/b] parameter here, 
  'so the current UserControl will be passed to the PageCollection.
End Sub
 
I don't understand why you've made a new collection of your own when you're not adding custom objects - why not just use the Controls collection that's already there?
 
ah I see the CollectionBase is the propertys for the Class but the controls are contained within the Usercontro.Controlsl not the CollectionBase

Is that right?

Cheers
 
will that allow me to change panels within the property window?

Bear in mind very new to collections etc having a play
 
A CollectionBase based class simply implements ICollection, IList, and
IEnumerable so that you can add objects to them and enumerate
them. The idea of typed collections is that you can make the 'Add'
function, etc. accept a type of object other than 'Object'. In the case of
Panel (which is a sub-class of Control), a typed-collection already
exists (System.Windows.Forms.ControlCollection). The Control collection
of the UserControl uses this type of collection and so will indeed work
in your case.
 
Back
Top