Condividi tramite


Evento NamedRange.SelectionChange

Si verifica quando viene modificata la selezione all'interno del controllo NamedRange.

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

Sintassi

'Dichiarazione
Event SelectionChange As DocEvents_SelectionChangeEventHandler
event DocEvents_SelectionChangeEventHandler SelectionChange

Note

Questo evento viene generato quando la selezione viene modificata nel controllo NamedRange o all'interno di esso.

Esempi

Nell'esempio di codice riportato di seguito viene illustrato un gestore per l'evento SelectionChange che visualizza l'indirizzo di una cella selezionata nel controllo NamedRange.

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

    Private selectionChangeRange As Microsoft.Office.Tools.Excel.NamedRange

    Private Sub DisplaySelectionChange()
        selectionChangeRange = Me.Controls.AddNamedRange( _
            Me.Range("A1", "E5"), "selectionChangeRange")
        AddHandler Me.selectionChangeRange.SelectionChange, _
            AddressOf selectionChangeRange_SelectionChange
    End Sub


    Sub selectionChangeRange_SelectionChange(ByVal Target As _
        Microsoft.Office.Interop.Excel.Range)
        Dim selectedRange As String = Target.Address(, , _
            Excel.XlReferenceStyle.xlA1, , )

        MessageBox.Show(Me.Name & ": " & selectedRange & _
            " raised the SelectionChange event.")
    End Sub

Microsoft.Office.Tools.Excel.NamedRange selectionChangeRange;
private void DisplaySelectionChange()
{
    selectionChangeRange = this.Controls.AddNamedRange(
        this.Range["A1", "E5"], "selectionChangeRange");
    this.selectionChangeRange.SelectionChange += new 
        Microsoft.Office.Interop.Excel.
        DocEvents_SelectionChangeEventHandler(
        selectionChangeRange_SelectionChange);
}

void selectionChangeRange_SelectionChange(
    Microsoft.Office.Interop.Excel.Range Target)
{
    string selectedRange = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);

    MessageBox.Show(this.Name + ": " + selectedRange + 
        " raised the SelectionChange event.");
}

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

Private selectionChangeRange As NamedRange

Private Sub DisplaySelectionChange()

    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Globals.ThisAddIn.Application.ActiveSheet

    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)

    selectionChangeRange = vstoWorksheet.Controls.AddNamedRange( _
        vstoWorksheet.Range("A1", "E5"), "selectionChangeRange")
    AddHandler Me.selectionChangeRange.SelectionChange, _
        AddressOf selectionChangeRange_SelectionChange
End Sub


Sub selectionChangeRange_SelectionChange(ByVal Target As  _
    Microsoft.Office.Interop.Excel.Range)
    Dim selectedRange As String = Target.Address(, , _
        Excel.XlReferenceStyle.xlA1, , )

    System.Windows.Forms.MessageBox.Show( _
        Me.Application.ActiveWorkbook.Worksheets(1).Name & _
        ": " & selectedRange & " raised the SelectionChange event.")
End Sub
NamedRange selectionChangeRange;
private void DisplaySelectionChange()
{
    Worksheet vstoWorksheet =
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    selectionChangeRange = vstoWorksheet.Controls.AddNamedRange(
        vstoWorksheet.Range["A1", "E5"], "selectionChangeRange");
    this.selectionChangeRange.SelectionChange += new
        Excel.DocEvents_SelectionChangeEventHandler(
        selectionChangeRange_SelectionChange);
}

void selectionChangeRange_SelectionChange(
    Microsoft.Office.Interop.Excel.Range Target)
{
    string selectedRange = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);

    Excel.Worksheet nativeWorksheet = (Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1];
    System.Windows.Forms.MessageBox.Show(
        nativeWorksheet.Name + ": " + selectedRange +
        " raised the SelectionChange event.");
}

Sicurezza di .NET Framework

Vedere anche

Riferimenti

NamedRange Interfaccia

Spazio dei nomi Microsoft.Office.Tools.Excel