[2008] UserControl Item Collection
Hi,
I am trying to create a UserControl that mimics the Options screen from the Visual Studio (2008) IDE and many other programs: a list of all the 'sub-options' which give you the corresponding options when selected.
My idea was that it worked pretty much the same as a TabControl except that the 'tab headers' are now the list entries and the tabpages are simple Panels that get hidden/unhidden when they are needed.
Now, I want the user to be able to add options panels (or "Pages") from the designer so he doesn't have to do it via code obviously.
I created a Public Property called Pages() of type "Collection" that will hold the Pages (which are of another UserControl control type called "ctrlPage", inherited from the Panel control).
I can access this property from the designer, but I can only add 'Objects'. How can I specify that these Pages are actually of the type "ctrlPage" instead of just Object?
I have tried replacing the Collection with a "List(Of ctrlPage)" but that keeps giving me an error about Panels and ctrlPage not being serializable or something...
The pages property is just this:
Private _Pages As Collection
vb.net Code:
Public Property Pages() As Collection
Get
If _Pages Is Nothing Then _Pages = New Collection()
Return _Pages
End Get
Set(ByVal value As Collection)
_Pages = value
End Set
End Property
Re: [2008] UserControl Item Collection
EDIT
Nevermind that, I found something else.
EDIT2 (See bold notes)
Instead of creating the Pages of type Collection, I created a new class (called clsPageCollection) inheriting from 'CollectionBase':
vb.net Code:
Public Class clsPageCollection
Inherits CollectionBase
Default Public Property Item(ByVal index As Integer) As ctrlPage
Get
Return CType(List(index), ctrlPage)
End Get
Set(ByVal value As ctrlPage)
List(index) = value
End Set
End Property
Public Function Add(ByVal value As ctrlPage) As Integer
Return List.Add(value)
End Function
End Class
I am getting closer now: in the Pages collection of my UserControl in the designer, I can now see the properties of the default Page (which I add by code).
However, when I click the 'Add' button to add a new page it says:
Constructor on type 'Options.ctrlPage' not found.
(It does not say this anymore)
Now, if I can remember correctly, the Constructor is the "Public Sub New()" sub that calls the InitializeComponent method, right?
If so, it is definitely there...?:
vb.net Code:
Public Class ctrlPage
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Dock = DockStyle.Fill
Me.Name = "Default"
Me.Controls.Add(Label1)
Label1.Top = 0
Label1.Left = 0
Label1.Text = "Default"
End Sub
End Class
What is going on?
I don't know why but it is working now!
However, when I use the designer to add a few pages, the 'Form1.Designer.vb' code comes out wrong... I have no control over this code creating, have I? so what can I do about this?
It looks like this (the error is simply "Syntax error")
vb.net Code:
'
'CtrlOptions1
'
Me.CtrlOptions1.Location = New System.Drawing.Point(12, 12)
Me.CtrlOptions1.Name = "CtrlOptions1"
New Options.clsPageCollection.Add(Me.CtrlPage1) 'error here
New Options.clsPageCollection.Add(Me.CtrlPage2) 'error here
Me.CtrlOptions1.Size = New System.Drawing.Size(407, 405)
Me.CtrlOptions1.TabIndex = 0
It seems to be exactly what is described here:
http://www.experts-exchange.com/Prog..._23214852.html
But since I have no subscription to this site I cannot see the solutions... :(
Re: [2008] UserControl Item Collection
The Collection class is a holdover from VB6 that should not be used at all in new VB.NET code.
The CollectionBase class was introduced in .NET 1.0 but became redundant in .NET 2.0 with the introduction of generics and the System.Collections.ObjectModel.Collection(Of T) class. In your case the code should be:
vb.net Code:
Public Class PageCollection
Inherits System.Collections.ObjectModel.Collection(Of Page)
End Class
That's it. All the standard collection functionality is inherited so there's no need to declare your own Item property or Add method. If you do want to add custom functionality, like validation on addition, then the Collection(Of T) class provides protected members you can override for that.
Re: [2008] UserControl Item Collection
Thanks for clarifying that. I thought the Collection was new in VB.NET lol, never seen it in VB6!
Anyway, when I do that, the objects in my Pages collection (in the designer) are simply "Objects" once again, and there are no properties I can set.
When I add the Item() and Add() methods like this, it does have properties again like usual, BUT the Syntax Error I described above appears again...
vb.net Code:
Public Class clsPageCollection
Inherits System.Collections.ObjectModel.Collection(Of ctrlPage)
Default Public Overloads Property Item(ByVal index As Integer) As ctrlPage
Get
Return MyBase.Item(index)
End Get
Set(ByVal value As ctrlPage)
MyBase.Item(index) = value
End Set
End Property
Public Overloads Sub Add(ByVal value As ctrlPage)
MyBase.Add(value)
End Sub
End Class
Re: [2008] UserControl Item Collection
There's no need or reason for you to be defining those members. I specifically said in my last post:
Quote:
That's it. All the standard collection functionality is inherited so there's no need to declare your own Item property or Add method.
Your class already has an Item property and an Add method simply by inheriting the Collection(Of T) class. I suggest that you read the documentation for the class, which you really should be doing when using new classes anyway.
As for design-time support, it's up to you to add that yourself. Your property is a custom type and its items are a custom type, so the designer isn't going to just whip up an editor for you. You have to design your own editor and then tell the designer to use it for your property by applying the appropriate attribute(s).
Re: [2008] UserControl Item Collection
Yes, and I tried that, I just had a 'blank' class that only inherited the Collection(Of T) class.
When I used that, I was back to my first problem: the objects in the 'collection designer' (I don't know what it's called, but it's the same thing you can use to add TabPages, or ListboxItems etc) were simply "Objects" and not "ctrlPages".
When I re-added those members they changed back to 'ctrlPages' and I could set any of the properties I have in the ctrlPage class.
(But then, the Syntax error comes up again).
I didn't have time yet to read the full documentation yet but I skimmed through it but didn't see anything related to my problem so quickly.