Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > Pro VB.NET 2002/2003
|
Pro VB.NET 2002/2003 For advanced Visual Basic coders working .NET version 2002/2003. Beginning-level questions will be redirected to other forums, including Beginning VB.NET.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2004, 06:27 PM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default urgent custom control with collection of controls

I am interested in someone providing a sample that has a panel on a user control that contains a collection of controls available at design time. To put it simply I would like the following.

1. a user control
2. a panel control
3. a collection of, let's say, label controls that are accessible at design time that are contained on the panel

I understand the method to add a collection, or any control, to the properties window is < DesignerSerializationVisibility(DesignerSerializat
ionVisibility.Content)>. What I don't understand is 1. How to create the collection of controls and where to place the < DesignerSerializationVisibility(DesignerSerializat
ionVisibility.Content)>. 2. How to place the controls on the Panel at design time.

BTW, I haven't found this simple example on any of the forums. I think Microsoft should provide a decent example of this.

A little extra information to clarify the need. I need a user control containing a panel with a collection of controls where the user control has the collection exposed in it's property window when it is dropped on a form. This will be much like the grid control, which has a columns property in it's property window. When the user clicks on the ellipse '...' an editor pops up where you can add and remove columns and set their properties.
 
Old December 19th, 2004, 06:05 PM
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, I was able to build the collection without help. Is there anyone that would care to tackle this problem? The code follows. The collections of buttons, labels, and textboxes are now exposed in the property window for the Usercontrol, code shown here. The problem is, when I add a control to the collection it is not apperant where it is placed. It shows up in code for the form in the Windows Generated Code section, but I can't locate it on the form or the Usercontrol.

The following code can be copied into a Usercontrol named WindowsControlLibrary2.

______________________________________________

Imports System.Windows.Forms

Imports System.Windows.Forms.Design

Imports System.ComponentModel

Imports System.ComponentModel.Design

Imports System.Drawing.Design

Imports System.Collections

Public Class UserControl1

Inherits System.Windows.Forms.UserControl

Private mButtons As ButtonCollection = New ButtonCollection

Private mLabels As LabelCollection = New LabelCollection

Private mTextBoxes As TextCollection = New TextCollection

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

mButtons = New ButtonCollection

mLabels = New LabelCollection

mTextBoxes = New TextCollection

End Sub

'UserControl1 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.

Friend WithEvents Panel1 As System.Windows.Forms.Panel

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.Panel1 = New System.Windows.Forms.Panel

Me.SuspendLayout()

'

'Panel1

'

Me.Panel1.Location = New System.Drawing.Point(24, 312)

Me.Panel1.Name = "Panel1"

Me.Panel1.Size = New System.Drawing.Size(568, 320)

Me.Panel1.TabIndex = 0

'

'UserControl1

'

Me.Controls.Add(Me.Panel1)

Me.Name = "UserControl1"

Me.Size = New System.Drawing.Size(688, 416)

Me.ResumeLayout(False)

End Sub

#End Region

<Editor(GetType(CollectionEditor), GetType(UITypeEditor)), _

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)> _

Public Property sfButtons() As ButtonCollection

Set(ByVal Value As ButtonCollection)

mButtons = Value

End Set

Get

Return mButtons

End Get

End Property

<Editor(GetType(CollectionEditor), GetType(UITypeEditor)), _

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)> _

Public Property sfLabels() As LabelCollection

Set(ByVal Value As LabelCollection)

mLabels = Value

End Set

Get

Return mLabels

End Get

End Property

<Editor(GetType(CollectionEditor), GetType(UITypeEditor)), _

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)> _

Public Property sfTextBoxes() As TextCollection

Set(ByVal Value As TextCollection)

mTextBoxes = Value

End Set

Get

Return mTextBoxes

End Get

End Property

End Class



<Serializable()> _

Public Class ButtonCollection

Inherits CollectionBase

Public Sub New()

MyBase.New()

End Sub

Public Sub New(ByVal value As ButtonCollection)

MyBase.New()

Me.AddRange(value)

End Sub

Public Sub New(ByVal value() As Button)

MyBase.New()

Me.AddRange(value)

End Sub

Default Public Property Item(ByVal index As Integer) As System.Windows.Forms.Button

Get

Return CType(List(index), System.Windows.Forms.Button)

End Get

Set(ByVal Value As System.Windows.Forms.Button)

List(index) = Value

End Set

End Property

Public Function Add(ByVal value As Button) As Integer

Return List.Add(value)

End Function

Public Overloads Sub AddRange(ByVal value() As Button)

Dim i As Integer = 0

Do While (i < value.Length)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Overloads Sub AddRange(ByVal value As ButtonCollection)

Dim i As Integer = 0

Do While (i < value.Count)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Function Contains(ByVal value As System.Windows.Forms.Button) As Boolean

Return List.Contains(value)

End Function

Public Sub CopyTo(ByVal array() As Button, ByVal index As Integer)

List.CopyTo(array, index)

End Sub

Public Function IndexOf(ByVal value As Button) As Integer

Return List.IndexOf(value)

End Function

Public Sub Insert(ByVal index As Integer, ByVal value As Button)

List.Insert(index, value)

End Sub

Public Shadows Function GetEnumerator() As ButtonEnumerator

Return New ButtonEnumerator(Me)

End Function

Public Sub Remove(ByVal value As Button)

List.Remove(value)

End Sub

Public Class ButtonEnumerator

Inherits Object

Implements IEnumerator

Private baseEnumerator As IEnumerator

Private temp As IEnumerable

Public Sub New(ByVal mappings As ButtonCollection)

MyBase.New()

Me.temp = CType(mappings, IEnumerable)

Me.baseEnumerator = temp.GetEnumerator

End Sub

Public ReadOnly Property Current() As System.Windows.Forms.Button

Get

Return CType(baseEnumerator.Current, System.Windows.Forms.Button)

End Get

End Property

ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current

Get

Return baseEnumerator.Current

End Get

End Property

Public Function MoveNext() As Boolean

Return baseEnumerator.MoveNext

End Function

Function IEnumerator_MoveNext() As Boolean Implements IEnumerator.MoveNext

Return baseEnumerator.MoveNext

End Function

Public Sub Reset()

baseEnumerator.Reset()

End Sub

Sub IEnumerator_Reset() Implements IEnumerator.Reset

baseEnumerator.Reset()

End Sub

End Class

End Class

<Serializable()> _

Public Class LabelCollection

Inherits CollectionBase

Public Sub New()

MyBase.New()

End Sub

Public Sub New(ByVal value As LabelCollection)

MyBase.New()

Me.AddRange(value)

End Sub

Public Sub New(ByVal value() As Label)

MyBase.New()

Me.AddRange(value)

End Sub

Default Public Property Item(ByVal index As Integer) As System.Windows.Forms.Label

Get

Return CType(List(index), System.Windows.Forms.Label)

End Get

Set(ByVal Value As System.Windows.Forms.Label)

List(index) = Value

End Set

End Property

Public Function Add(ByVal value As Label) As Integer

Return List.Add(value)

End Function

Public Overloads Sub AddRange(ByVal value() As Label)

Dim i As Integer = 0

Do While (i < value.Length)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Overloads Sub AddRange(ByVal value As LabelCollection)

Dim i As Integer = 0

Do While (i < value.Count)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Function Contains(ByVal value As System.Windows.Forms.Label) As Boolean

Return List.Contains(value)

End Function

Public Sub CopyTo(ByVal array() As Label, ByVal index As Integer)

List.CopyTo(array, index)

End Sub

Public Function IndexOf(ByVal value As Label) As Integer

Return List.IndexOf(value)

End Function

Public Sub Insert(ByVal index As Integer, ByVal value As Label)

List.Insert(index, value)

End Sub

Public Shadows Function GetEnumerator() As LabelEnumerator

Return New LabelEnumerator(Me)

End Function

Public Sub Remove(ByVal value As Label)

List.Remove(value)

End Sub

Public Class LabelEnumerator

Inherits Object

Implements IEnumerator

Private baseEnumerator As IEnumerator

Private temp As IEnumerable

Public Sub New(ByVal mappings As LabelCollection)

MyBase.New()

Me.temp = CType(mappings, IEnumerable)

Me.baseEnumerator = temp.GetEnumerator

End Sub

Public ReadOnly Property Current() As System.Windows.Forms.Label

Get

Return CType(baseEnumerator.Current, System.Windows.Forms.Label)

End Get

End Property

ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current

Get

Return baseEnumerator.Current

End Get

End Property

Public Function MoveNext() As Boolean

Return baseEnumerator.MoveNext

End Function

Function IEnumerator_MoveNext() As Boolean Implements IEnumerator.MoveNext

Return baseEnumerator.MoveNext

End Function

Public Sub Reset()

baseEnumerator.Reset()

End Sub

Sub IEnumerator_Reset() Implements IEnumerator.Reset

baseEnumerator.Reset()

End Sub

End Class

End Class

<Serializable()> _

Public Class TextCollection

Inherits CollectionBase

Public Sub New()

MyBase.New()

End Sub

Public Sub New(ByVal value As TextCollection)

MyBase.New()

Me.AddRange(value)

End Sub

Public Sub New(ByVal value() As TextBox)

MyBase.New()

Me.AddRange(value)

End Sub

Default Public Property Item(ByVal index As Integer) As System.Windows.Forms.TextBox

Get

Return CType(List(index), System.Windows.Forms.TextBox)

End Get

Set(ByVal Value As System.Windows.Forms.TextBox)

List(index) = Value

End Set

End Property

Public Function Add(ByVal value As TextBox) As Integer

Return List.Add(value)

End Function

Public Overloads Sub AddRange(ByVal value() As TextBox)

Dim i As Integer = 0

Do While (i < value.Length)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Overloads Sub AddRange(ByVal value As TextCollection)

Dim i As Integer = 0

Do While (i < value.Count)

Me.Add(value(i))

i = (i + 1)

Loop

End Sub

Public Function Contains(ByVal value As System.Windows.Forms.TextBox) As Boolean

Return List.Contains(value)

End Function

Public Sub CopyTo(ByVal array() As TextBox, ByVal index As Integer)

List.CopyTo(array, index)

End Sub

Public Function IndexOf(ByVal value As TextBox) As Integer

Return List.IndexOf(value)

End Function

Public Sub Insert(ByVal index As Integer, ByVal value As TextBox)

List.Insert(index, value)

End Sub

Public Shadows Function GetEnumerator() As TextEnumerator

Return New TextEnumerator(Me)

End Function

Public Sub Remove(ByVal value As TextBox)

List.Remove(value)

End Sub

Public Class TextEnumerator

Inherits Object

Implements IEnumerator

Private baseEnumerator As IEnumerator

Private temp As IEnumerable

Public Sub New(ByVal mappings As TextCollection)

MyBase.New()

Me.temp = CType(mappings, IEnumerable)

Me.baseEnumerator = temp.GetEnumerator

End Sub

Public ReadOnly Property Current() As System.Windows.Forms.TextBox

Get

Return CType(baseEnumerator.Current, System.Windows.Forms.TextBox)

End Get

End Property

ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current

Get

Return baseEnumerator.Current

End Get

End Property

Public Function MoveNext() As Boolean

Return baseEnumerator.MoveNext

End Function

Function IEnumerator_MoveNext() As Boolean Implements IEnumerator.MoveNext

Return baseEnumerator.MoveNext

End Function

Public Sub Reset()

baseEnumerator.Reset()

End Sub

Sub IEnumerator_Reset() Implements IEnumerator.Reset

baseEnumerator.Reset()

End Sub

End Class

End Class

______________________________________________

Form1 code:

______________________________________________

Public Class Form1

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form 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.

Friend WithEvents UserControl11 As WindowsControlLibrary2.UserControl1

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.UserControl11 = New WindowsControlLibrary2.UserControl1

Me.SuspendLayout()

'

'UserControl11

'

Me.UserControl11.Location = New System.Drawing.Point(40, 56)

Me.UserControl11.Name = "UserControl11"

Me.UserControl11.Size = New System.Drawing.Size(688, 416)

Me.UserControl11.TabIndex = 0

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(760, 541)

Me.Controls.Add(Me.UserControl11)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)

End Sub

#End Region

End Class

______________________________________________







Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom collection Issue dhara_adh Classic ASP Professional 1 August 15th, 2007 01:10 AM
System.Web.UI.Control.Controls Collection Ihoulun ASP.NET 1.0 and 1.1 Basics 0 October 17th, 2006 07:51 AM
User Control within Custom Server Controls www2004 ASP.NET 1.x and 2.0 Application Design 3 April 15th, 2005 09:00 AM
DataGrid Controls Within custom validator control geetha_ganesan ADO.NET 1 March 26th, 2005 04:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.