I'm trying to write a COM add in for Excel in C# that disables the ability to double click a cell and then edit it, I want to popup a box saying that editing a cell this way is prohibited and then stop all execution.
Looking through the documentation at Microsoft this seems like a very easy task, you create an Application event AppEvents_SheetBeforeDoubleClickEventHandler
with the signature func(object sheet, Range Target, ref bool Cancel)
and you set Cancel
to true so that execution stops. However, this isn't happening.
I have the following:
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
Excel.Application app = application as Excel.Application;
app.SheetBeforeDoubleClick += Excel.AppEvents_SheetBeforeDoubleClickEventHandler(beforeDoubleClick);
}
void beforeDoubleClick(object sheet, Excel.Range Target, ref bool Cancel)
{
MessageBox.Show("Cannot edit cells this way sorry.");
Cancel = true;
}
The message box is shown, but the cell is then put into edit mode, however, if I have the same thing from VBA it works.
The exact same thing happens with a VB.NET addin using the following code.
Private WithEvents app As Excel.Application
Public Sub OnConnection(ByVal application As Object,
ByVal connectMode As Extensibility.ext_ConnectMode,
ByVal addInInst As Object,
ByRef custom As System.Array)
Implements Extensibility.IDTExtensibility2.OnConnection
app = application
End Sub
Sub Worksheet1_BeforeDoubleClick(ByVal obj As Object,
ByVal Target As Excel.Range,
ByRef Cancel As Boolean)
Handles app.SheetBeforeDoubleClick
MsgBox("Double-clicking in this sheet is not allowed.")
Cancel = True
End Sub
I'm using Excel XP :(