Control.ControlCollection.Contains(Control) 方法

定义

确定指定控件是否为集合的成员。Determines whether the specified control is a member of the collection.

public:
 bool Contains(System::Windows::Forms::Control ^ control);
public bool Contains (System.Windows.Forms.Control control);
member this.Contains : System.Windows.Forms.Control -> bool

参数

control
Control

要在集合中查找的 ControlThe Control to locate in the collection.

返回

如果 Control 是集合的成员,则为 true;否则为 falsetrue if the Control is a member of the collection; otherwise, false.

示例

下面的代码示例从派生类的 @no__t @no__t (如果它是集合的成员,则从 Panel)中删除一个0。The following code example removes a Control from the Control.ControlCollection of the derived class Panel if it is a member of the collection. 该示例要求您在 @no__t 3 上创建了一个 Panel、一个 Button 和至少一个 @no__t 2 控件。The example requires that you have created a Panel, a Button, and at least one RadioButton control on a Form. 将 @no__t 0 个控件添加到 @no__t 控件,并将 @no__t 2 控件添加到 @no__t。The RadioButton controls are added to the Panel control, and the Panel control added to the Form. 单击该按钮时,将从 @no__t 中删除名为 removeButton 的单选按钮。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

注解

此方法使你可以在尝试对 @no__t 执行操作之前确定 @no__t 0 是否为集合的成员。This method enables you to determine whether a Control is a member of the collection before attempting to perform operations on the Control. 您可以使用此方法确认已将 @no__t 0 添加到或仍是集合的成员。You can use this method to confirm that a Control has been added to or is still a member of the collection.

适用于

另请参阅