NamedRange.BeforeRightClick Event (2007 System)
Updated: July 2008
Occurs when a NamedRange control is right-clicked, before the default right-click action.
Assembly: Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)
Right-clicking overlapping NamedRange controls raises the event on each of the controls that overlap.
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.
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.
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; }
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.