Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

Control.Controls Property

Gets the collection of controls contained within the control.

[Visual Basic]
Public ReadOnly Property Controls As Control.ControlCollection
[C#]
public Control.ControlCollection Controls {get;}
[C++]
public: __property Control.ControlCollection* get_Controls();
[JScript]
public function get Controls() : Control.ControlCollection;

Property Value

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 object assigned to the Controls property of the form, which is derived from the Control class.

You can manipulate the controls in the Control.ControlCollection object 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.

Example

[Visual Basic, C#, C++] The following example removes a Control from the Control.ControlCollection of the derived class Panel if it is a member of the collection. The example assumes 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);
   }
}

[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);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Control Class | Control Members | System.Windows.Forms Namespace | Control.ControlCollection | CreateControlsInstance | SuspendLayout | ResumeLayout

Show:
© 2015 Microsoft