Control Properties


.NET Framework Class Library
Control..::.Controls Property

Gets the collection of controls contained within the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public ReadOnly Property Controls As Control..::.ControlCollection
Visual Basic (Usage)
Dim instance As Control
Dim value As Control..::.ControlCollection

value = instance.Controls
C#
[BrowsableAttribute(false)]
public Control..::.ControlCollection Controls { get; }
Visual C++
[BrowsableAttribute(false)]
public:
property Control..::.ControlCollection^ Controls {
    Control..::.ControlCollection^ get ();
}
JScript
public function get Controls () : Control..::.ControlCollection

Property Value

Type: System.Windows.Forms..::.Control..::.ControlCollection
A Control..::.ControlCollection representing the collection of controls contained within the control.
Remarks

A Control can act as a parent to a collection of controls. For example, when several controls are added to a Form, each of the controls is a member of the Control..::.ControlCollection assigned to the Controls property of the form, which is derived from the Control class.

You can manipulate the controls in the Control..::.ControlCollection assigned to the Controls property by using the methods available in the Control..::.ControlCollection class.

When adding several controls to a parent control, it is recommended that you call the SuspendLayout method before initializing the controls to be added. After adding the controls to the parent control, call the ResumeLayout method. Doing so will increase the performance of applications with many controls.

Use the Controls property to iterate through all controls of a form, including nested controls. Use the GetNextControl method to retrieve the previous or next child control in the tab order. Use the ActiveControl property to get or set the active control of a container control.

Examples

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 control(s) 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.

Visual Basic
    ' Remove the RadioButton control if it exists.
    Private Sub RemoveButton_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles RemoveButton.Click
        If Panel1.Controls.Contains(RadioAddRangeButton) Then
            Panel1.Controls.Remove(RadioAddRangeButton)
        End If
    End Sub
C#
// 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);
   }
}
Visual C++
   // 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 );
      }
   }
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Tags :


Community Content

Manfred Lange
Iterating over all Child Controls
The documentation states: "Use the Controls property to iterate through all controls of a form, including nested controls."

This doesn't seem to be quite right unless "nested" means something different than child controls. The following code does not iterate over child controls:
foreach(Control ctrl in parentControl.Controls) {
...
}

In order to access all of the controls, including nested/child controls, use this code:

public void Foo(Control.ControlCollection controls) {

foreach(Control ctrl in controls) {

...
Foo(ctrl.Controls);
}
}

Admittedly, this is recursive code. Generally speaking this should be avoided in order to avoid stack overruns. However, in this particular case it is probably acceptable since the number of levels in any form should be fairly limited.

Tags :

Page view tracker