共用方式為


NamedRange.BeforeRightClick 事件

NamedRange 控制項上按一下滑鼠右鍵時 (在預設的按一下滑鼠右鍵動作之前) 便會發生。

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

語法

'宣告
Event BeforeRightClick As DocEvents_BeforeRightClickEventHandler
event DocEvents_BeforeRightClickEventHandler BeforeRightClick

備註

以滑鼠右鍵按一下重疊的 NamedRange 控制項,會在每個重疊的控制項上引發事件。

範例

下列程式碼範例會建立 NamedRange,然後以文字 Delete 填入所有儲存格。 若要測試事件,以滑鼠右鍵按一下其中一個儲存格,使框線出現在範圍四周,然後按兩下其中一個儲存格,清除範圍。

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

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

Private Sub ClickToChangeRange()
    clickRange = Me.Controls.AddNamedRange(Me.Range("B2", "D4"), _
        "clickRange")
    clickRange.Value2 = "Delete"
    AddHandler clickRange.BeforeDoubleClick, _
        AddressOf clickRange_BeforeDoubleClick
    AddHandler clickRange.BeforeRightClick, _
        AddressOf clickRange_BeforeRightClick
End Sub


Sub clickRange_BeforeRightClick(ByVal Target As Excel.Range, _
    ByRef Cancel As Boolean)
    clickRange.BorderAround(, Excel.XlBorderWeight.xlThick, _
        Excel.XlColorIndex.xlColorIndexAutomatic, )
    Cancel = True
End Sub


Sub clickRange_BeforeDoubleClick(ByVal Target As _
    Excel.Range, ByRef Cancel As Boolean)
    clickRange.Clear()
    Cancel = True
End Sub
Microsoft.Office.Tools.Excel.NamedRange clickRange;
private void ClickToChangeRange()
{
    clickRange = this.Controls.AddNamedRange(
        this.Range["B2", "D4"], "clickRange");
    clickRange.Value2 = "Delete";
    clickRange.BeforeDoubleClick += new 
        Microsoft.Office.Interop.Excel.
        DocEvents_BeforeDoubleClickEventHandler(
        clickRange_BeforeDoubleClick);
    clickRange.BeforeRightClick += new 
        Microsoft.Office.Interop.Excel.
        DocEvents_BeforeRightClickEventHandler(
        clickRange_BeforeRightClick);
}

void clickRange_BeforeRightClick(
    Excel.Range Target, ref bool Cancel)
{
    clickRange.BorderAround(missing, Excel.XlBorderWeight.xlThick,
        Excel.XlColorIndex.xlColorIndexAutomatic, missing);
    Cancel = true;
}

void clickRange_BeforeDoubleClick(
    Excel.Range Target, ref bool Cancel)
{
    clickRange.Clear();
    Cancel = true;
}

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

Private clickRange As NamedRange

Private Sub ClickToChangeRange()

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

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

    clickRange = vstoWorksheet.Controls.AddNamedRange( _
    vstoWorksheet.Range("B2", "D4"), _
        "clickRange")
    clickRange.Value2 = "Delete"
    AddHandler clickRange.BeforeDoubleClick, _
        AddressOf clickRange_BeforeDoubleClick
    AddHandler clickRange.BeforeRightClick, _
        AddressOf clickRange_BeforeRightClick
End Sub


Sub clickRange_BeforeRightClick(ByVal Target As Excel.Range, _
    ByRef Cancel As Boolean)
    clickRange.BorderAround(, Excel.XlBorderWeight.xlThick, _
        Excel.XlColorIndex.xlColorIndexAutomatic, )
    Cancel = True
End Sub


Sub clickRange_BeforeDoubleClick(ByVal Target As  _
    Excel.Range, ByRef Cancel As Boolean)
    clickRange.Clear()
    Cancel = True
End Sub
NamedRange clickRange;
private void ClickToChangeRange()
{
    Worksheet vstoWorksheet =
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    clickRange = vstoWorksheet.Controls.AddNamedRange(
        vstoWorksheet.Range["B2", "D4"], "clickRange");
    clickRange.Value2 = "Delete";
    clickRange.BeforeDoubleClick += new
        Excel.DocEvents_BeforeDoubleClickEventHandler(
        clickRange_BeforeDoubleClick);
    clickRange.BeforeRightClick += new
        Excel.DocEvents_BeforeRightClickEventHandler(
        clickRange_BeforeRightClick);
}

void clickRange_BeforeRightClick(
    Excel.Range Target, ref bool Cancel)
{
    clickRange.BorderAround(missing, Excel.XlBorderWeight.xlThick,
        Excel.XlColorIndex.xlColorIndexAutomatic, missing);
    Cancel = true;
}

void clickRange_BeforeDoubleClick(
    Excel.Range Target, ref bool Cancel)
{
    clickRange.Clear();
    Cancel = true;
}

.NET Framework 安全性

請參閱

參考

NamedRange 介面

Microsoft.Office.Tools.Excel 命名空間