a_jam_sandwich Posted March 31, 2003 Posted March 31, 2003 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 Quote Code today gone tomorrow!
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 I dont quite get what you mean the Panels will be container as in normal heres the source 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 Quote Code today gone tomorrow!
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 Just to add I know theres no remove Quote Code today gone tomorrow!
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 Now im confused any examples? Quote Code today gone tomorrow!
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 OK, in your PageCollection class, add a member variable:Private _parentUserControl As UserControland change the constructor of the PageCollection as well:Public Sub New(parent As UserControl) _parentUserControl = parent End SubThis will store the control that the class is going to be used with, thus exposing its Control collection. Now, change the Add method:Public Function Add(ByVal Item As Panel) As Integer _parentUserControl.Controls.Add(Item) Return List.Add(Item) End FunctionThat will add the panel to the UserControl. Finally, you need to change the constructor of the PageList class: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 Quote
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 Wow I will now study to under stand Thx very much Andrew Quote Code today gone tomorrow!
*Gurus* divil Posted March 31, 2003 *Gurus* Posted March 31, 2003 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? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 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 Quote Code today gone tomorrow!
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 will that allow me to change panels within the property window? Bear in mind very new to collections etc having a play Quote Code today gone tomorrow!
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 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. Quote
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 So this would also work for indepenant user controls exactly the same as a form? Quote Code today gone tomorrow!
*Experts* Volte Posted March 31, 2003 *Experts* Posted March 31, 2003 I think it is pretty much the same, yes. Quote
a_jam_sandwich Posted March 31, 2003 Author Posted March 31, 2003 Thank you both Quote Code today gone tomorrow!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.