Share via


Button.Index 屬性

取得 Button 在工作表上 OLEObjects 集合內的索引編號。

命名空間:  Microsoft.Office.Tools.Excel.Controls
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
Public ReadOnly Property Index As Integer
public int Index { get; }

屬性值

型別:System.Int32
Button 在工作表上 OLEObjects 集合內的索引編號。

備註

將控制項加入至 Microsoft Office Excel 工作表時,索引值是從 2 開始,因為該索引是工作表上 OLEObjects 集合的索引。

範例

下列程式碼範例會將三個 Button 控制項加入至工作表。 這些按鈕使用同一個 Click 事件處理常式,該處理常式會使用 Index 屬性顯示所按下按鈕的索引。

這是示範文件層級自訂的範例。

Private Sub DisplayIndex()
    Dim IndexButton1 As Microsoft.Office.Tools.Excel.Controls.Button = _
        Me.Controls.AddButton(Me.Range("B2", "C3"), "IndexButton1")
    IndexButton1.Text = "Click to view the index"
    AddHandler IndexButton1.Click, AddressOf indexButton_Click

    Dim IndexButton2 As Microsoft.Office.Tools.Excel.Controls.Button = _
        Me.Controls.AddButton(Me.Range("B5", "C6"), "IndexButton2")
    IndexButton2.Text = "Click to view the index"
    AddHandler IndexButton2.Click, AddressOf indexButton_Click

    Dim IndexButton3 As Microsoft.Office.Tools.Excel.Controls.Button = _
        Me.Controls.AddButton(Me.Range("B8", "C9"), "IndexButton3")
    IndexButton3.Text = "Click to view the index"
    AddHandler IndexButton3.Click, AddressOf indexButton_Click
End Sub

Private Sub IndexButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim ClickedButton As Microsoft.Office.Tools.Excel.Controls.Button = _
        CType(sender, Microsoft.Office.Tools.Excel.Controls.Button)

    MsgBox("The index is " & ClickedButton.Index.ToString())
End Sub
private void DisplayIndex()
{
    Microsoft.Office.Tools.Excel.Controls.Button indexButton1 =
            this.Controls.AddButton(this.Range["B2", "C3"],
            "indexButton1");
    indexButton1.Text = "Click to view the index";
    indexButton1.Click += new EventHandler(indexButton_Click);

    Microsoft.Office.Tools.Excel.Controls.Button indexButton2 =
            this.Controls.AddButton(this.Range["B5", "C6"],
            "indexButton2");
    indexButton2.Text = "Click to view the index";
    indexButton2.Click += new EventHandler(indexButton_Click);

    Microsoft.Office.Tools.Excel.Controls.Button indexButton3 =
            this.Controls.AddButton(this.Range["B8", "C9"],
            "indexButton3");
    indexButton3.Text = "Click to view the index";
    indexButton3.Click += new EventHandler(indexButton_Click);
}

void indexButton_Click(object sender, EventArgs e)
{
    Microsoft.Office.Tools.Excel.Controls.Button clickedButton =
        (Microsoft.Office.Tools.Excel.Controls.Button)sender;

    MessageBox.Show("The index is " +
        clickedButton.Index.ToString());
}

.NET Framework 安全性

請參閱

參考

Button 類別

Microsoft.Office.Tools.Excel.Controls 命名空間