Results 1 to 6 of 6

Thread: [2008] UserControl Item Collection

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    [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:
    1. Public Property Pages() As Collection
    2.         Get
    3.             If _Pages Is Nothing Then _Pages = New Collection()
    4.             Return _Pages
    5.         End Get
    6.         Set(ByVal value As Collection)
    7.             _Pages = value
    8.         End Set
    9.     End Property

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Public Class clsPageCollection
    2.     Inherits CollectionBase
    3.  
    4.     Default Public Property Item(ByVal index As Integer) As ctrlPage
    5.         Get
    6.             Return CType(List(index), ctrlPage)
    7.         End Get
    8.         Set(ByVal value As ctrlPage)
    9.             List(index) = value
    10.         End Set
    11.     End Property
    12.  
    13.     Public Function Add(ByVal value As ctrlPage) As Integer
    14.         Return List.Add(value)
    15.     End Function
    16.  
    17. 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:
    1. Public Class ctrlPage
    2.  
    3.     Public Sub New()
    4.  
    5.         ' This call is required by the Windows Form Designer.
    6.         InitializeComponent()
    7.  
    8.         ' Add any initialization after the InitializeComponent() call.
    9.         Me.Dock = DockStyle.Fill
    10.         Me.Name = "Default"
    11.  
    12.         Me.Controls.Add(Label1)
    13.         Label1.Top = 0
    14.         Label1.Left = 0
    15.         Label1.Text = "Default"
    16.     End Sub
    17.  
    18. 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:
    1. '
    2.         'CtrlOptions1
    3.         '
    4.         Me.CtrlOptions1.Location = New System.Drawing.Point(12, 12)
    5.         Me.CtrlOptions1.Name = "CtrlOptions1"
    6. New Options.clsPageCollection.Add(Me.CtrlPage1)    'error here
    7. New Options.clsPageCollection.Add(Me.CtrlPage2)    'error here
    8.         Me.CtrlOptions1.Size = New System.Drawing.Size(407, 405)
    9.         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...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class PageCollection
    2.     Inherits System.Collections.ObjectModel.Collection(Of Page)
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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:
    1. Public Class clsPageCollection
    2.     Inherits System.Collections.ObjectModel.Collection(Of ctrlPage)
    3.  
    4.     Default Public Overloads Property Item(ByVal index As Integer) As ctrlPage
    5.         Get
    6.             Return MyBase.Item(index)
    7.         End Get
    8.         Set(ByVal value As ctrlPage)
    9.             MyBase.Item(index) = value
    10.         End Set
    11.     End Property
    12.  
    13.     Public Overloads Sub Add(ByVal value As ctrlPage)
    14.         MyBase.Add(value)
    15.     End Sub
    16.  
    17. End Class

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width