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

返品

true Controlがコレクションのメンバーである場合は ɴ。それ以外の場合はfalse

次のコード例では、Controlがコレクションのメンバーである場合にControl.ControlCollection派生クラスのPanelから削除します。 この例では、PanelButtonRadioButton、および少なくとも 1 つのForm コントロールを作成している必要があります。 RadioButton コントロールがPanel コントロールに追加され、Panel コントロールがFormに追加されます。 ボタンをクリックすると、 removeButton という名前のラジオ ボタンが 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 がコレクションに追加されているか、コレクションのメンバーであることを確認できます。

適用対象

こちらもご覧ください