Control.ControlCollection 类
表示 Control 对象的集合。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
<ComVisibleAttribute(False)> _
Public Class ControlCollection
Inherits ArrangedElementCollection
Implements IList, ICollection, IEnumerable, ICloneable
用法
Dim instance As ControlCollection
[ComVisibleAttribute(false)]
public class ControlCollection : ArrangedElementCollection, IList, ICollection, IEnumerable,
ICloneable
[ComVisibleAttribute(false)]
public ref class ControlCollection : public ArrangedElementCollection, IList, ICollection, IEnumerable,
ICloneable
/** @attribute ComVisibleAttribute(false) */
public class ControlCollection extends ArrangedElementCollection implements IList, ICollection,
IEnumerable, ICloneable
ComVisibleAttribute(false)
public class ControlCollection extends ArrangedElementCollection implements IList, ICollection,
IEnumerable, ICloneable
备注
Add、Remove 和 RemoveAt 方法使您能够在集合中添加和移除单个控件。还可以使用 AddRange 或 Clear 方法在集合中添加或移除所有控件。
将控件传入 Contains 方法,可以确定 Control 是否为集合的成员。若要获取集合中的 Control 位置的索引值,请将控件传入 IndexOf 方法。通过调用 CopyTo 方法,可以将集合复制到数组中。
示例
下面的代码示例从派生类 Panel 的 Control.ControlCollection 中移除一个 Control(如果它是该集合的成员)。该示例要求已在 Form 上创建了一个 Panel、一个 Button 以及至少一个 RadioButton 控件。将 RadioButton 控件添加到 Panel 控件,而将 Panel 控件添加到 Form。单击该按钮时,从 Control.ControlCollection 中移除名为 radioButton2
的单选按钮。
' 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
// 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);
}
}
// 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 );
}
}
// Remove the RadioButton control if it exists.
private void removeButton_Click(Object sender, System.EventArgs e)
{
if (panel1.get_Controls().Contains(removeButton)) {
panel1.get_Controls().Remove(removeButton);
}
} //removeButton_Click
继承层次结构
System.Object
System.Windows.Forms.Layout.ArrangedElementCollection
System.Windows.Forms.Control.ControlCollection
派生类
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
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 成员
System.Windows.Forms 命名空间
Control 类
IList