Control.ControlCollection.Clear 方法

从集合中移除所有控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overridable Sub Clear
用法
Dim instance As ControlCollection

instance.Clear
public virtual void Clear ()
public:
virtual void Clear ()
public void Clear ()
public function Clear ()

备注

可以使用 Clear 方法从父控件中移除整个控件集合。

若要从集合中移除单个控件,请使用 RemoveRemoveAt 方法。

Note重要事项:

调用 Clear 方法不会从内存中移除控件句柄。必须显式调用 Dispose 方法才能避免内存泄漏。

若要向集合中添加新的 Control 对象,请使用 AddAddRange 方法。

给继承者的说明 在派生类中重写 Clear 时,要确保调用基类的 Clear 方法,以保证从集合中移除所有控件。

示例

下面的代码示例移除派生类 PanelControl.ControlCollection 中的所有 Control 对象。该示例要求已在 Form 上创建了一个 Panel、一个 Button 以及至少一个其他控件。将其他控件添加到 Panel 控件,而将 Panel 添加到 Form。单击该按钮后,面板中包含的所有控件将从 Control.ControlCollection 中移除。

' 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
// Clear all the controls in the Panel.
private void clearButton_Click(object sender, System.EventArgs e)
{
   panel1.Controls.Clear();
}
   // Clear all the controls in the Panel.
private:
   void clearButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      panel1->Controls->Clear();
   }
// Clear all the controls in the Panel.
private void clearButton_Click(Object sender, System.EventArgs e)
{
    panel1.get_Controls().Clear();
} //clearButton_Click

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Control.ControlCollection 类
Control.ControlCollection 成员
System.Windows.Forms 命名空间