NamedRange.SelectionChange 事件 (2007 系統)

更新: 2008 年 7 月

會在 NamedRange 控制項內的選取範圍變更時發生。

命名空間: Microsoft.Office.Tools.Excel
組件: Microsoft.Office.Tools.Excel.v9.0 (在 Microsoft.Office.Tools.Excel.v9.0.dll 中)

語法

Public Event SelectionChange As DocEvents_SelectionChangeEventHandler

Dim instance As NamedRange
Dim handler As DocEvents_SelectionChangeEventHandler

AddHandler instance.SelectionChange, handler
public event DocEvents_SelectionChangeEventHandler SelectionChange

備註

當選取範圍變更為 NamedRange 控制項,或此控制項內的選取範圍有所變更時,便會引發這個事件。

範例

下列程式碼範例會示範 SelectionChange 事件的處理常式,顯示已於 NamedRange 中選取之儲存格的位址。

這是示範文件層級自訂的版本。

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

這是示範應用程式層級增益集的版本。

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

使用權限

請參閱

參考

NamedRange 類別

NamedRange 成員

Microsoft.Office.Tools.Excel 命名空間

變更記錄

日期

記錄

原因

2008 年 7 月

加入應用程式層級增益集的程式碼範例版本。

SP1 功能變更。