共用方式為


NamedRange.Deselected 事件 (2007 系統)

更新: 2008 年 7 月

會在選取範圍從 NamedRange 控制項移至 NamedRange 控制項以外的區域時發生。

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

語法

Public Event Deselected As DocEvents_SelectionChangeEventHandler

Dim instance As NamedRange
Dim handler As DocEvents_SelectionChangeEventHandler

AddHandler instance.Deselected, handler
public event DocEvents_SelectionChangeEventHandler Deselected

備註

只有在 NamedRange 控制項擁有焦點,而焦點又接著從 NamedRange 控制項移開時,才會引發這個事件。

範例

下列程式碼範例會示範 Deselected 事件的處理常式,顯示選取範圍所移至之儲存格的位址。

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

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

Private Sub DeselectRange()
    deselectedRange = Me.Controls.AddNamedRange( _
        Me.Range("B1", "D4"), "deselectedRange")
    deselectedRange.Select()
    AddHandler Me.deselectedRange.Deselected, _
        AddressOf deselectedRange_Deselected
End Sub


Sub deselectedRange_Deselected(ByVal Target As Excel.Range)
    Dim targetRange As String = Target.Address(, , _
        Excel.XlReferenceStyle.xlA1, , )

    MessageBox.Show("The NamedRange control was deselected." & _
        " Selection transferred to " & Me.Name & ":" & _
        targetRange & ".")
End Sub
Microsoft.Office.Tools.Excel.NamedRange deselectedRange;
private void DeselectRange()
{
    deselectedRange = this.Controls.AddNamedRange(
        this.Range["B1", "D4"], "deselectedRange");
    deselectedRange.Select();
    this.deselectedRange.Deselected += new
        Microsoft.Office.Interop.Excel.
        DocEvents_SelectionChangeEventHandler(
        deselectedRange_Deselected);
}

void deselectedRange_Deselected(Excel.Range Target)
{
    string targetRange = Target.get_Address(missing, missing,
        Excel.XlReferenceStyle.xlA1, missing, missing);

    MessageBox.Show("The NamedRange control was deselected." +
        " Selection transferred to " + this.Name + ":" + 
        targetRange + ".");
}

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

Private deselectedRange As NamedRange

Private Sub DeselectRange()
    Dim vstoWorksheet As Worksheet = CType( _
        Me.Application.ActiveWorkbook.Worksheets(1),  _
        Excel.Worksheet).GetVstoObject()
    deselectedRange = vstoWorksheet.Controls.AddNamedRange( _
        vstoWorksheet.Range("B1", "D4"), "deselectedRange")
    deselectedRange.Select()
    AddHandler Me.deselectedRange.Deselected, _
        AddressOf deselectedRange_Deselected
End Sub


Sub deselectedRange_Deselected(ByVal Target As Excel.Range)
    Dim targetRange As String = Target.Address(, , _
        Excel.XlReferenceStyle.xlA1, , )

    System.Windows.Forms.MessageBox.Show( _
        "The NamedRange control was deselected." & _
        " Selection transferred to " & _
        Me.Application.ActiveWorkbook.Worksheets(1).Name & ":" & _
        targetRange & ".")
End Sub
NamedRange deselectedRange;
private void DeselectRange()
{
    Worksheet vstoWorksheet = ((Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1]).GetVstoObject();
    deselectedRange = vstoWorksheet.Controls.AddNamedRange(
        vstoWorksheet.Range["B1", "D4"], "deselectedRange");
    deselectedRange.Select();
    deselectedRange.Deselected += new
        Excel.DocEvents_SelectionChangeEventHandler(
        deselectedRange_Deselected);
}

void deselectedRange_Deselected(Excel.Range Target)
{
    string targetRange = 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("The NamedRange control was deselected." +
        " Selection transferred to " +
        nativeWorksheet.Name + ":" +
        targetRange + ".");
}

使用權限

請參閱

參考

NamedRange 類別

NamedRange 成員

Microsoft.Office.Tools.Excel 命名空間

變更記錄

日期

記錄

原因

2008 年 7 月

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

SP1 功能變更。