The example code below is a complete macro to register an event handler:
/*
* Created by SharpDevelop.
* Date: 8/26/2018
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
//note: add System.Windows.Forms as a reference so MessageBox can be used.
//note; A task dialog box will cause an error if it is called while another dialog box is displayed.
//note: so its better not to use them inside an event handler
namespace EventHandler
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("4B2CEE66-8082-4EA5-B510-58BF7A1227B1")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
// declaring an event handler here will start the handler as soon as revit opens
// the dialog box showing event handler is a member of the UIControlledApplication
//DialogBoxShowing += new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
}
private void Module_Shutdown(object sender, EventArgs e)
{
//if the handler is registered on startup, unregister on shutdown
//DialogBoxShowing -= new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
}
// the event handler body goes here
//notice that neither private or public is listed
//also notice the use of void for no return object witch is different from most external commands or applications
void AppDialogShowing(object sender, Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs args)
{System.Windows.Forms.MessageBox.Show("Dialog Events Box - Message Box Shows up First");
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void DiagBoxEventsExample()
{
//typical startup objects that might be needed for various macros
Document doc = this.ActiveUIDocument.Document;
UIApplication uiapp = new UIApplication(Application);
UIDocument uidoc = this.ActiveUIDocument;
//register the event handler:
//note that this handler only catches some dialog boxes (macro manager, view range, view graphics,...)
//and not other (none of the objects are visible in the current view, Approporiate Host not found....)
DialogBoxShowing += new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
//once the handler is registered, run a task dialog to trigger the event
// the messagebox in the event handler will appear before the task dialog below
TaskDialog.Show("Revit", "Task Dialog Shows up Second");
//unregister the handler
DialogBoxShowing -= new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
}