This topic has not yet been rated - Rate this topic

Control.ControlCollection Class

Represents a collection of Control objects.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[ListBindableAttribute(false)]
[ComVisibleAttribute(false)]
public class ControlCollection : ArrangedElementCollection, 
	IList, ICollection, IEnumerable, ICloneable

The Control.ControlCollection type exposes the following members.

  Name Description
Public method Control.ControlCollection Initializes a new instance of the Control.ControlCollection class.
Top
  Name Description
Public property Count Gets the number of elements in the collection. (Inherited from ArrangedElementCollection.)
Public property IsReadOnly Gets a value indicating whether the collection is read-only. (Inherited from ArrangedElementCollection.)
Public property Item[Int32] Indicates the Control at the specified indexed location in the collection.
Public property Item[String] Indicates a Control with the specified key in the collection.
Public property Owner Gets the control that owns this Control.ControlCollection.
Top
  Name Description
Public method Add Adds the specified control to the control collection.
Public method AddRange Adds an array of control objects to the collection.
Public method Clear Removes all controls from the collection.
Public method Contains Determines whether the specified control is a member of the collection.
Public method ContainsKey Determines whether the Control.ControlCollection contains an item with the specified key.
Public method CopyTo Copies the entire contents of this collection to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from ArrangedElementCollection.)
Public method Equals Determines whether two ArrangedElementCollection instances are equal. (Inherited from ArrangedElementCollection.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method Find Searches for controls by their Name property and builds an array of all the controls that match.
Public method GetChildIndex(Control) Retrieves the index of the specified child control within the control collection.
Public method GetChildIndex(Control, Boolean) Retrieves the index of the specified child control within the control collection, and optionally raises an exception if the specified control is not within the control collection.
Public method GetEnumerator Retrieves a reference to an enumerator object that is used to iterate over a Control.ControlCollection. (Overrides ArrangedElementCollection.GetEnumerator().)
Public method GetHashCode Returns the hash code for this instance. (Inherited from ArrangedElementCollection.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IndexOf Retrieves the index of the specified control in the control collection.
Public method IndexOfKey Retrieves the index of the first occurrence of the specified item within the collection.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Remove Removes the specified control from the control collection.
Public method RemoveAt Removes a control from the control collection at the specified indexed location.
Public method RemoveByKey Removes the child control with the specified key.
Public method SetChildIndex Sets the index of the specified child control in the collection to the specified index value.
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Cast<TResult> Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  Name Description
Explicit interface implemetation Private method ICloneable.Clone Infrastructure. For a description of this member, see ICloneable.Clone.
Explicit interface implemetation Private property ICollection.IsSynchronized For a description of this member, see the ICollection.IsSynchronized property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private property ICollection.SyncRoot For a description of this member, see the ICollection.SyncRoot property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private method IList.Add Infrastructure. For a description of this member, see IList.Add.
Explicit interface implemetation Private method IList.Clear For a description of this member, see the IList.Clear method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private method IList.Contains For a description of this member, see the IList.Contains method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private method IList.IndexOf For a description of this member, see the IList.IndexOf method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private method IList.Insert For a description of this member, see the IList.Insert method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private property IList.IsFixedSize For a description of this member, see the IList.IsFixedSize property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private property IList.Item For a description of this member, see the IList.Item property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetation Private method IList.Remove Infrastructure. For a description of this member, see IList.Remove.
Explicit interface implemetation Private method IList.RemoveAt For a description of this member, see the IList.RemoveAt method. (Inherited from ArrangedElementCollection.)
Top

The Add, Remove, and RemoveAt methods enable you to add and remove individual controls from the collection. You can also use the AddRange or Clear methods to add or remove all the controls from the collection.

You can determine if a Control is a member of the collection by passing the control into the Contains method. To get the index value of the location of a Control in the collection, pass the control into the IndexOf method. The collection can be copied into an array by calling the CopyTo method.

The following code example removes a Control from the Control.ControlCollection of the derived class Panel if it is a member of the collection. The example requires that you have created a Panel, a Button, and at least one RadioButton control on a Form. The RadioButton controls are added to the Panel control, and the Panel control added to the Form. When the button is clicked, the radio button named radioButton2 is removed from the Control.ControlCollection.


// Remove the RadioButton control if it exists.
private void removeButton_Click(object sender, System.EventArgs e)
{
   if(panel1.Controls.Contains(removeButton))
   {
      panel1.Controls.Remove(removeButton);
   }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Error in example?
Shouldn't it be

if(panel1.Controls.Contains(radiobutton2))
   {
      panel1.Controls.Remove(radiobutton2);