ListObject.Change 事件 (2007 system)

更新: 2008 年 7 月

ListObject 控件中的某项内容更改时发生。

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

语法

声明
Public Event Change As ListObjectChangeHandler
用法
Dim instance As ListObject
Dim handler As ListObjectChangeHandler

AddHandler instance.Change, handler
public event ListObjectChangeHandler Change

备注

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

当单元格在重新计算过程中发生更改时,不会发生此事件。

示例

下面的代码示例创建一个 ListObject 和一个 Change 事件处理程序。若要引发 Change 事件,请向 ListObject 中的某个单元格添加文本,然后按 Enter。

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

WithEvents ChangeList As Microsoft.Office.Tools.Excel.ListObject
Private Sub ListObject_Change()
    ChangeList = Me.Controls.AddListObject( _
        Me.Range("A1", "C4"), "ChangeList")
End Sub


Sub List1_Change(ByVal targetRange As _
    Microsoft.Office.Interop.Excel.Range, _
    ByVal changedRanges As Microsoft.Office.Tools.Excel.ListRanges) _
    Handles ChangeList.Change

    Dim cellAddress As String = targetRange.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)

    Select Case changedRanges
        Case Microsoft.Office.Tools.Excel.ListRanges.DataBodyRange
            MsgBox("The cells at range " & cellAddress & _
                " in the data body changed.")
        Case Microsoft.Office.Tools.Excel.ListRanges.HeaderRowRange
            MsgBox("The cells at range " & cellAddress & _
                " in the header row changed.")
        Case Microsoft.Office.Tools.Excel.ListRanges.TotalsRowRange
            MsgBox("The cells at range " & cellAddress & _
                " in the totals row changed.")
        Case Else
            MsgBox("The cells at range " & cellAddress & _
                " changed.")
    End Select
End Sub
private void ListObject_Change()
{
    Microsoft.Office.Tools.Excel.ListObject list1 = 
        this.Controls.AddListObject(
        this.Range["A1", "C4"], "list1");
    list1.Change += new Microsoft.Office.Tools.Excel.
        ListObjectChangeHandler(list1_Change);
}

void list1_Change(Microsoft.Office.Interop.Excel.Range 
    targetRange, Microsoft.Office.Tools.Excel.ListRanges 
    changedRanges)
{
    string cellAddress = targetRange.get_Address(
        missing, missing, Excel.XlReferenceStyle.xlA1, 
        missing, missing);

    switch (changedRanges)
    {
        case Microsoft.Office.Tools.Excel.ListRanges.DataBodyRange:
            MessageBox.Show("The cells at range " + cellAddress +
                " in the data body changed.");
            break;
        case Microsoft.Office.Tools.Excel.ListRanges.HeaderRowRange:
            MessageBox.Show("The cells at range " + cellAddress +
                " in the header row changed.");
            break;
        case Microsoft.Office.Tools.Excel.ListRanges.TotalsRowRange:
            MessageBox.Show("The cells at range " + cellAddress +
                " in the totals row changed.");
            break;
        default:
            MessageBox.Show("The cells at range " + cellAddress +
                " changed.");
            break;
    }
}

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

WithEvents ChangeList As ListObject
Private Sub ListObject_Change()
    Dim vstoWorksheet As Worksheet = CType( _
        Me.Application.ActiveWorkbook.Worksheets(1),  _
        Excel.Worksheet).GetVstoObject()
    ChangeList = vstoWorksheet.Controls.AddListObject( _
        vstoWorksheet.Range("A1", "C4"), "ChangeList")
End Sub


Sub List1_Change(ByVal targetRange As  _
    Microsoft.Office.Interop.Excel.Range, _
    ByVal changedRanges As Microsoft.Office.Tools.Excel.ListRanges) _
    Handles ChangeList.Change

    Dim cellAddress As String = targetRange.Address( _
        ReferenceStyle:=Excel.XlReferenceStyle.xlA1)

    Select Case changedRanges
        Case ListRanges.DataBodyRange
            MsgBox("The cells at range " & cellAddress & _
                " in the data body changed.")
        Case ListRanges.HeaderRowRange
            MsgBox("The cells at range " & cellAddress & _
                " in the header row changed.")
        Case ListRanges.TotalsRowRange
            MsgBox("The cells at range " & cellAddress & _
                " in the totals row changed.")
        Case Else
            MsgBox("The cells at range " & cellAddress & _
                " changed.")
    End Select
End Sub
private void ListObject_Change()
{
    Worksheet vstoWorksheet = ((Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1]).GetVstoObject();
    ListObject list1 =
        vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1", "C4"], "list1");
    list1.Change += new ListObjectChangeHandler(list1_Change);
}

void list1_Change(Microsoft.Office.Interop.Excel.Range
    targetRange, Microsoft.Office.Tools.Excel.ListRanges
    changedRanges)
{
    string cellAddress = targetRange.get_Address(
        missing, missing, Excel.XlReferenceStyle.xlA1,
        missing, missing);

    switch (changedRanges)
    {
        case ListRanges.DataBodyRange:
            System.Windows.Forms.MessageBox.Show("The cells at range " + cellAddress +
                " in the data body changed.");
            break;
        case ListRanges.HeaderRowRange:
            System.Windows.Forms.MessageBox.Show("The cells at range " + cellAddress +
                " in the header row changed.");
            break;
        case ListRanges.TotalsRowRange:
            System.Windows.Forms.MessageBox.Show("The cells at range " + cellAddress +
                " in the totals row changed.");
            break;
        default:
            System.Windows.Forms.MessageBox.Show("The cells at range " + cellAddress +
                " changed.");
            break;
    }
}

权限

另请参见

参考

ListObject 类

ListObject 成员

Microsoft.Office.Tools.Excel 命名空间

修订记录

日期

修订记录

原因

2008 年 7 月

添加了一个针对应用程序级外接程序的代码示例版本。

SP1 功能更改。