Export (0) Print
Expand All

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.

  NameDescription
Public methodControl.ControlCollectionInitializes a new instance of the Control.ControlCollection class.
Top

  NameDescription
Public propertyCountGets the number of elements in the collection. (Inherited from ArrangedElementCollection.)
Public propertyIsReadOnlyGets a value indicating whether the collection is read-only. (Inherited from ArrangedElementCollection.)
Public propertyItem[Int32]Indicates the Control at the specified indexed location in the collection.
Public propertyItem[String]Indicates a Control with the specified key in the collection.
Public propertyOwnerGets the control that owns this Control.ControlCollection.
Top

  NameDescription
Public methodAddAdds the specified control to the control collection.
Public methodAddRangeAdds an array of control objects to the collection.
Public methodClearRemoves all controls from the collection.
Public methodContainsDetermines whether the specified control is a member of the collection.
Public methodContainsKeyDetermines whether the Control.ControlCollection contains an item with the specified key.
Public methodCopyToCopies 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 methodEqualsDetermines whether two ArrangedElementCollection instances are equal. (Inherited from ArrangedElementCollection.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodFindSearches for controls by their Name property and builds an array of all the controls that match.
Public methodGetChildIndex(Control)Retrieves the index of the specified child control within the control collection.
Public methodGetChildIndex(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 methodGetEnumeratorRetrieves a reference to an enumerator object that is used to iterate over a Control.ControlCollection. (Overrides ArrangedElementCollection.GetEnumerator().)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from ArrangedElementCollection.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfRetrieves the index of the specified control in the control collection.
Public methodIndexOfKeyRetrieves the index of the first occurrence of the specified item within the collection.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified control from the control collection.
Public methodRemoveAtRemoves a control from the control collection at the specified indexed location.
Public methodRemoveByKeyRemoves the child control with the specified key.
Public methodSetChildIndexSets the index of the specified child control in the collection to the specified index value.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodICloneable.CloneInfrastructure. For a description of this member, see ICloneable.Clone.
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedFor a description of this member, see the ICollection.IsSynchronized property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate propertyICollection.SyncRootFor a description of this member, see the ICollection.SyncRoot property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate methodIList.AddInfrastructure. For a description of this member, see IList.Add.
Explicit interface implemetationPrivate methodIList.ClearFor a description of this member, see the IList.Clear method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate methodIList.ContainsFor a description of this member, see the IList.Contains method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate methodIList.IndexOfFor a description of this member, see the IList.IndexOf method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate methodIList.InsertFor a description of this member, see the IList.Insert method. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate propertyIList.IsFixedSizeFor a description of this member, see the IList.IsFixedSize property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate propertyIList.ItemFor a description of this member, see the IList.Item property. (Inherited from ArrangedElementCollection.)
Explicit interface implemetationPrivate methodIList.RemoveInfrastructure. For a description of this member, see IList.Remove.
Explicit interface implemetationPrivate methodIList.RemoveAtFor 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.

Community Additions

ADD
Show:
© 2014 Microsoft