ControlCollection.Contains 方法

定义

确定指定的控件是否为 ControlCollection 实例的成员。

重载

Contains(String)

确定具有指定名称的控件是否为 ControlCollection 实例的成员。

Contains(Object)

确定指定的控件是否为 ControlCollection 实例的成员。

注解

此方法使你可以在尝试对控件执行操作之前确定控件是否为集合的成员。 您可以使用此方法来确认控件是否已添加到实例,或仍是实例的成员 ControlCollection

Contains(String)

确定具有指定名称的控件是否为 ControlCollection 实例的成员。

public bool Contains (string name);

参数

name
String

要在 ControlCollection 实例中搜索的控件的名称。

返回

Boolean

如果在集合中找到了该控件,则为 true;否则为 false

适用于

Contains(Object)

确定指定的控件是否为 ControlCollection 实例的成员。

public bool Contains (object control);

参数

control
Object

要在 ControlCollection 实例中搜索的控件。

返回

Boolean

如果在集合中找到了该控件,则为 true;否则为 false

示例

下面的代码示例将 Button 控件添加到工作表的顶部,然后在消息框中显示该按钮的索引。

private void ExcelControlIndexOf()
{

    Microsoft.Office.Tools.Excel.Controls.Button button1 =
        this.Controls.AddButton(0, 0, 56.25, 17.25,
        "button1");
    button1.Text = "OK";

    if (this.Controls.Contains(button1))
    {
        MessageBox.Show("The index of button1 is " +
            Controls.IndexOf(button1));
    }
}
Private Sub ExcelControlIndexOf()

    Dim Button1 As Microsoft.Office.Tools.Excel. _
        Controls.Button = Me.Controls.AddButton( _
        0, 0, 56.25, 17.25, "Button1")
    Button1.Text = "OK"

    If Me.Controls.Contains(Button1) Then
        MessageBox.Show("The index of Button1 is " _
        & Controls.IndexOf(Button1))
    End If

End Sub 

适用于