The problem you have is that once you add the handler for the executed event of the binding your code takes ownership of that command. This means that the question would be put to the user repeatedly (if you posted the same command ID as the one you are overriding). Note also that the command binding 'Delete' relates to the X button on the modify panel. There are other ways of deleting things that will not be captured, such as with right click delete in project browser.
You could try to remove the command binding but then after the post command completes you can not reattach it, you are out of context.
Some commands can be cancelled by setting the event arguments property 'cancel' to true but delete doesn't fall into this category it seems.
I believe this awkward design pattern exists because they expect you to provide your own full implementation of the command rather than intercepting the command and preventing it under certain circumstances. Would be good if there was a PostOriginalCommand function so you could always revert to the original built in command at the end of your command.
Implementing you own delete command is quite straightforward so you could do instead:
using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class EntryAppClass : IExternalApplication
{
private AddInCommandBinding DeleteCMDBinding = null;
private bool Cancel = false;
private void CheckDeleteFirst(object s, Autodesk.Revit.UI.Events.BeforeExecutedEventArgs e)
{
Cancel = false;
if (TaskDialog.Show("Confirm delete", "Are you sure you want to delete?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No) == TaskDialogResult.No)
{
Cancel = true;
}
}
private void CancelDelete(object s, Autodesk.Revit.UI.Events.ExecutedEventArgs e)
{
if (Cancel)
{
//Nothing to do
}
else
{
UIApplication UIApp = s as UIApplication;
Document D = UIApp.ActiveUIDocument.Document;
using (Transaction Tx = new Transaction(D, "Delete after check"))
{
if (Tx.Start() == TransactionStatus.Started)
{
D.Delete(UIApp.ActiveUIDocument.Selection.GetElementIds());
Tx.Commit();
}
}
}
}
private void ToggleAddinCMDBinding(UIControlledApplication App, bool OnShutDown = false)
{
try
{
if (DeleteCMDBinding == null == false)
{
DeleteCMDBinding.BeforeExecuted -= CheckDeleteFirst;
DeleteCMDBinding.Executed -= CancelDelete;
App.RemoveAddInCommandBinding(RevitCommandId.LookupPostableCommandId(PostableCommand.Delete));
DeleteCMDBinding = null;
}
else
{
if (OnShutDown == false)
{
DeleteCMDBinding = App.CreateAddInCommandBinding(RevitCommandId.LookupPostableCommandId(PostableCommand.Delete));
DeleteCMDBinding.BeforeExecuted += CheckDeleteFirst;
DeleteCMDBinding.Executed += CancelDelete;
}
}
}
catch (Exception ex)
{
//if command already bound this will fail
}
}
public Result OnShutdown(Autodesk.Revit.UI.UIControlledApplication application)
{
ToggleAddinCMDBinding(application, true);
return Autodesk.Revit.UI.Result.Succeeded;
}
public Result OnStartup(Autodesk.Revit.UI.UIControlledApplication application)
{
ToggleAddinCMDBinding(application);
return Autodesk.Revit.UI.Result.Succeeded;
}
}