Hi,
I want my application to display a message on opening a new document and on saving the active document.
So, in the Initialize method, I register a DocumentCollection.DocumentCreated and a Database.BeginSave one for all already opened documents.
The DocumentCreated callback method registers a Database.BeginSave for the new Document.Databse.
My questions are:
1- Do I need to unregister the DocumentCollection.DocumentCreated in the terminate method ?
2- Do I need to add a DocumentCollection.DocumentToBeDestroyed event with a callback to unregister the Database.BeginSave of this document ?
3- If so, do I need to unregister the DocumentCollection.DocumentToBeDestroyed in the Terminate method too ?
Here's an extract of what I wrote.
{code}
private DocumentCollection docMan = acadApp.DocumentManager;
public void Initialize()
{
docMan.DocumentCreated += new DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed += new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
foreach (Document doc in docMan)
{
doc.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
}
AddDefaultContextMenu();
AddObjectContextMenu();
ShowMessage();
}
public void Terminate()
{
acadApp.RemoveDefaultContextMenuExtension(dcme);
RXClass rxc = Entity.GetClass(typeof(Entity));
acadApp.RemoveObjectContextMenuExtension(rxc, ocme);
try
{
docMan.DocumentCreated -= new DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Terminte: " + ex.Message);
}
}
private void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
{
if (e.Document == null) return;
e.Document.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
ShowMessage();
}
private void OnDocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
Document doc = e.Document;
try
{
doc.Database.BeginSave -= new DatabaseIOEventHandler(OnSaveAlert);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("DocumentToBeDestroyed: " + ex.Message);
}
}{code}