Evento ListObject.Change

Si verifica quando si apportano modifiche nel controllo ListObject.

Spazio dei nomi:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Sintassi

'Dichiarazione
Event Change As ListObjectChangeHandler
event ListObjectChangeHandler Change

Note

Questo evento viene generato se il controllo ListObject cambia in seguito a una modifica a livello di codice o all'interazione con l'utente.

Questo evento non si verifica quando le celle cambiano durante un'operazione di ricalcolo.

Esempi

Nell'esempio di codice riportato di seguito viene creato un controllo ListObject e un gestore per l'evento Change. Per generare l'evento Change, aggiungere del testo in una delle celle del controllo ListObject, quindi premere INVIO.

Questa versione è valida per una personalizzazione a livello di documento.

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;
    }
}

Questa versione è valida per un componente aggiuntivo a livello di applicazione.

WithEvents ChangeList As ListObject
Private Sub ListObject_Change()
    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Me.Application.Worksheets(1)
    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)
    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 =
Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    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;
    }
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

ListObject Interfaccia

Spazio dei nomi Microsoft.Office.Tools.Excel