Share via


NamedRange.BeforeRightClick Event (2007 System)

Occurs when a NamedRange control is right-clicked, before the default right-click action.

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

Syntax

'Declaration
Public Event BeforeRightClick As DocEvents_BeforeRightClickEventHandler
'Usage
Dim instance As NamedRange 
Dim handler As DocEvents_BeforeRightClickEventHandler 

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

Remarks

Right-clicking overlapping NamedRange controls raises the event on each of the controls that overlap.

Examples

The following code example creates a NamedRange and then populates all the cells with the text Delete. To test the events, right-click one of the cells to make a border appear around the range, and then double-click one of the cells to clear the range.

This version is for a document-level customization.

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;
}

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

Private clickRange As NamedRange

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