ListObject.SelectedIndexChanged 事件

SelectedIndex 属性的值更改时发生。

命名空间:  Microsoft.Office.Tools.Excel
程序集:  Microsoft.Office.Tools.Excel(在 Microsoft.Office.Tools.Excel.dll 中)

语法

声明
Event SelectedIndexChanged As EventHandler
event EventHandler SelectedIndexChanged

备注

如果选择了标题行、插入行或总计行,将不引发此事件。

如果通过编程修改或用户交互更改 SelectedIndex 属性,则将引发此事件。

示例

下面的代码示例在当前的工作表上创建一个 ListObject。 它使用 SelectedIndexAutoSelectRows 属性选择第二行并指定在用户单击某一行时将选择整行。 该代码示例还为在选择新行时显示消息框的 SelectedIndexChanged 事件创建事件处理程序。

此版本针对的是文档级自定义项。

    WithEvents SelectedIndexList As Microsoft.Office.Tools.Excel.ListObject
    Private Sub ListObject_SelectedIndex()
        SelectedIndexList = _
            Me.Controls.AddListObject(Me.Range("A1", "D4"), _
            "SelectedIndexList")
    SelectedIndexList.AutoSelectRows = True
        SelectedIndexList.SelectedIndex = 2
    End Sub


    Private Sub List1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As EventArgs) Handles SelectedIndexList.SelectedIndexChanged
        MessageBox.Show("Selected index has changed.")
    End Sub

private void ListObject_SelectedIndex()
{
    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(this.Range["A1", "D4"],
        "list1");

    list1.AutoSelectRows = true;
    list1.SelectedIndex = 2;
    list1.SelectedIndexChanged +=
        new EventHandler(list1_SelectedIndexChanged);
}

void list1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show("Selected index has changed.");
}

此版本针对的是应用程序级外接程序。

    WithEvents SelectedIndexList As ListObject
    Private Sub ListObject_SelectedIndex()

        Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
            Me.Application.Worksheets(1)
        Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
                Globals.Factory.GetVstoObject(NativeWorksheet)
        SelectedIndexList = _
            vstoWorksheet.Controls.AddListObject( _
            vstoWorksheet.Range("A1", "D4"), _
            "SelectedIndexList")
        SelectedIndexList.AutoSelectRows = True
        SelectedIndexList.SelectedIndex = 2
    End Sub


    Private Sub List1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As EventArgs) Handles SelectedIndexList.SelectedIndexChanged
        System.Windows.Forms.MessageBox.Show("Selected index has changed.")
    End Sub

private void ListObject_SelectedIndex()
{
    Worksheet vstoWorksheet = 
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    ListObject list1 =
        vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1", "D4"],
        "list1");

    list1.AutoSelectRows = true;
    list1.SelectedIndex = 2;
    list1.SelectedIndexChanged +=
        new EventHandler(list1_SelectedIndexChanged);
}

void list1_SelectedIndexChanged(object sender, EventArgs e)
{
    System.Windows.Forms.MessageBox.Show("Selected index has changed.");
}

.NET Framework 安全性

请参见

参考

ListObject 接口

Microsoft.Office.Tools.Excel 命名空间