NamedRange.SelectionChange Event (2007 System)

Occurs when the selection inside the NamedRange control changes.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

Syntax

'Declaration
Public Event SelectionChange As DocEvents_SelectionChangeEventHandler
'Usage
Dim instance As NamedRange 
Dim handler As DocEvents_SelectionChangeEventHandler 

AddHandler instance.SelectionChange, handler
public event DocEvents_SelectionChangeEventHandler SelectionChange
public:
 event DocEvents_SelectionChangeEventHandler^ SelectionChange {
    void add (DocEvents_SelectionChangeEventHandler^ value);
    void remove (DocEvents_SelectionChangeEventHandler^ value);
}
JScript does not support events.

Remarks

This event is raised when the selection changes to or within the NamedRange control.

Examples

The following code example demonstrates a handler for the SelectionChange event that displays the address of a cell that is selected in the NamedRange.

This version is for a document-level customization.

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.");
}

This version is for an application-level add-in.

Private selectionChangeRange As NamedRange

Private Sub DisplaySelectionChange()
    Dim vstoWorksheet As Worksheet = CType( _
        Me.Application.ActiveWorkbook.Worksheets(1),  _
        Excel.Worksheet).GetVstoObject()
    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 = ((Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1]).GetVstoObject();
    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.");
}

.NET Framework Security

See Also

Reference

NamedRange Class

NamedRange Members

Microsoft.Office.Tools.Excel Namespace

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.