ControlCollection.IndexOf 方法

定义

重载

IndexOf(Object)

搜索指定控件,并返回该控件在 ControlCollection 实例中的第一个匹配项的从零开始的索引。

IndexOf(String)

搜索具有指定名称的控件在 ControlCollection 实例中的第一个匹配项。

IndexOf(Object)

搜索指定控件,并返回该控件在 ControlCollection 实例中的第一个匹配项的从零开始的索引。

public int IndexOf (object control);

参数

control
Object

索引待查找的控件。

返回

Int32

控件的索引,如果控件不在 ControlCollection 实例中,则为 -1。

示例

下面的代码示例将 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 

适用于

IndexOf(String)

搜索具有指定名称的控件在 ControlCollection 实例中的第一个匹配项。

public int IndexOf (string name);

参数

name
String

要查找其索引的控件的名称。

返回

Int32

一个表示控件索引的整数,如果控件不在 ControlCollection 实例中,则为 -1。

适用于