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

範例

如果衍生類別是集合的成員,下列程式碼範例會 ControlControl.ControlCollection 衍生類別 Panel 的 移除 。 此範例會要求您已在 上 Form 建立 、 PanelButton 和 至少一個 RadioButton 控制項。 控制項 RadioButton 會新增至 Panel 控制項,以及 Panel 加入至 的 Form 控制項。 按一下按鈕時,會從 Control.ControlCollection 中移除名為 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.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 已新增 至 或 仍是集合的成員。

適用於

另請參閱