Control.ControlCollection.Contains(Control) 方法

定义

确定指定控件是否为集合的成员。

public:
 bool Contains(System::Windows::Forms::Control ^ control);
public bool Contains (System.Windows.Forms.Control control);
public bool Contains (System.Windows.Forms.Control? control);
member this.Contains : System.Windows.Forms.Control -> bool
Public Function Contains (control As Control) As Boolean

参数

control
Control

要在集合中查找的 Control

返回

Boolean

如果 Control 是集合的成员,则为 true;否则为 false

示例

如果派生类是集合的成员,则以下代码示例从派生类Panel中删除 Control Control.ControlCollection a。 该示例要求你已创建一个Panel、一个Button和至少一个RadioButton控件。Form 控件 RadioButton 将添加到 Panel 控件,以及 Panel 添加到控件中的 Form控件。 When the button is clicked, the radio button named removeButton is removed from the Control.ControlCollection.

   // 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 Sub RemoveButton_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles RemoveButton.Click
    If Panel1.Controls.Contains(RemoveButton) Then
        Panel1.Controls.Remove(RemoveButton)
    End If
End Sub

注解

通过此方法,可以在尝试对Control集合执行操作之前确定某个Control集合是否是集合的成员。 可以使用此方法确认 Control 已添加到集合中还是仍然是集合的成员。

适用于

另请参阅