Export (0) Print
Expand All

Control.ControlCollection.Clear Method

Removes all controls from the collection.

[Visual Basic]
Public Overridable Sub Clear() Implements IList.Clear
[C#]
public virtual void Clear();
[C++]
public: virtual void Clear();
[JScript]
public function Clear();

Implements

IList.Clear

Remarks

You can use the Clear method to remove the entire collection of controls from a parent control.

To remove an individual control from the collection, use the Remove or RemoveAt methods.

To add new Control objects to the collection, use the Add or AddRange methods.

Notes to Inheritors:  When overriding Clear in a derived class, be sure to call the base class's Clear method to ensure that all the controls are removed from the collection.

Example

[Visual Basic, C#, C++] The following example removes all the Control objects in the Control.ControlCollection of the derived class Panel. The example assumes you have created a Panel, a Button, and at least one other control on a Form. The other control(s) are added to the Panel control, and the Panel added to the Form. When the button is clicked, all the controls contained in the panel are removed from the Control.ControlCollection.

[Visual Basic] 
' Clear all the controls in the Panel.
Private Sub ClearButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles ClearButton.Click
    Panel1.Controls.Clear()
End Sub

[C#] 
// Clear all the controls in the Panel.
private void clearButton_Click(object sender, System.EventArgs e)
{
   panel1.Controls.Clear();
}

[C++] 
// Clear all the controls in the Panel.
private:
void clearButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
    panel1->Controls->Clear();
}

[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.ControlCollection Class | Control.ControlCollection Members | System.Windows.Forms Namespace

Show:
© 2015 Microsoft