Clear Method

Control.ControlCollection.Clear Method

Removes all controls from the collection.

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




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.


[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
End Sub

// Clear all the controls in the Panel.
private void clearButton_Click(object sender, System.EventArgs e)

// Clear all the controls in the Panel.
void clearButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {

[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.


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

© 2016 Microsoft