Sorry, I didn't realize that you were using a modeless form. In that case, you can dispense with the seperate class for the events, but only if you do not allow your form to Dispose itself when the user closes it (I showed how to do this in another post here, but here it is again):
protected override void OnFormClosing( FormClosingEventArgs e )
{
base.OnFormClosing( e );
e.Cancel = true;
this.Visible = false;
}
This will ensure that when the user closes your form it will not be disposed, and you can reference the same instance of the form for the life of your application. In that case, you should check the form's Visible property and avoid doing things that you shouldn't or don't need to do if the form is not currently visible.
Given that your form is modeless and a singleton, there's nothing wrong with making the event handlers methods of the form. So you should probably dispense with the use of a seperate class, and make the event handlers members of your form, and call the code to add the DocumentCollection event handlers from an override of your form's OnShown() method (just move the methods of your InitiateDocEventHandlers class to the form):
public class MyForm : Form
{
proctected override OnShown( EventArgs e )
{
base.OnShown( e );
AddEventHandlersToDocsOpen(); // make this a member of your form
}
// ......
}
Your form is effectively a 'singleton' (Wiki the 'Singleton pattern' for more about what a singleton is). When you need to reference a singleton object from the outside, you use the singleton pattern, which generally involves exposing the single instance of the class through a static property, like this:
public class MyForm : Form
{
private static MyForm instance = null;
public static MyForm Instance
{
get
{
if( instance == null )
instance = new MyForm();
return instance;
}
}
}
With the above, if you need to reference your form from another class, you can do that with this:
MyForm myForm = MyForm.Instance;
And so, the CommandMethod that shows the form only needs to do this:
(using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;)
AcadApp.ShowModelessDialog( MyForm.Instance );
BTW, using 'System.Windows.Forms.Application.OpenForms["DialogMain"]' to reference a form is definitely not a good idea (it may not necessarily return the same instance of the form).
Lastly, consider not needlessly complicating your code with the direct use of IEnumerable methods. You generlly don't use IEnumerable directly, instead you just use foreach().
The only other question I have, is why use a modeless form, rather than a PaletteSet ? PaletteSets have some big advantages over modeless forms.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000 through 2009
http://www.acadxtabs.com
Introducing AcadXTabs 2010:
http://www.caddzone.com/acadxtabs/AcadXTabs2010.htm
wrote in message news:6018233@discussion.autodesk.com...
Okay,
This is how I did the event handling:
public class PartManager1
: Autodesk.AutoCAD.Runtime.IExtensionApplication
{
public void Initialize()
{
InitiateDocEventHandlers InitEventHandlers = new InitiateDocEventHandlers();
InitEventHandlers.AddEventHandlersToDocsOpen();
}
public void Terminate()
{
Console.WriteLine("Cleaning up...");
}
[CommandMethod("Partmanager", CommandFlags.Session)]
public static void ShowModelessDialog()
{
if (MainDialog == null)
MainDialog = new DialogMain();
if(!MainDialog.Visible)
Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(MainDialog);
}
private static DialogMain MainDialog = null;
}
}
//***This is what the InitiateEventHandlers looks like:***
namespace PartMan
{
public class InitiateDocEventHandlers
{
public void AddEventHandlersToDocsOpen()
{
try
{
DocumentCollection docCol = AcadApp.DocumentManager;
IEnumerator docEnum = docCol.GetEnumerator();
while (docEnum.MoveNext())
{
Document doc = (Document)docEnum.Current;
doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);
}
AcadApp.DocumentManager.DocumentActivated += new DocumentCollectionEventHandler(DocumentManager_DocumentActivated);
AcadApp.DocumentManager.DocumentCreated += new DocumentCollectionEventHandler(DocumentManager_DocumentCreated);
}
catch (System.Exception e)
{
MessageBox.Show("\n EventHandlers kaputt! " + e);
}
}
//Methods for event handlers here....
void doc_CommandEnded(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.Equals("INSERT"))
{
DialogMain dlgmn = (DialogMain)System.Windows.Forms.Application.OpenForms["DialogMain"];
dlgmn.RefreshListAfterInsert();
}
}
void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
{
AcadApp.DocumentManager.MdiActiveDocument.CommandEnded += new CommandEventHandler(MdiActiveDocument_CommandEnded);
}
void MdiActiveDocument_CommandEnded(object sender, CommandEventArgs e)
{
if (e.GlobalCommandName.Equals("INSERT"))
{
DialogMain dlgmn = (DialogMain)System.Windows.Forms.Application.OpenForms["DialogMain"];
dlgmn.RefreshListAfterInsert();
}
}
public void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
{
DialogMain dlgmn = (DialogMain)System.Windows.Forms.Application.OpenForms["DialogMain"];
dlgmn.RefreshListWhenDrawingActivated();
}
}
}
i'm sure there are still 100 things wrong with this so if anyone can see some glaring deficiencies, keep it short. 🙂
Thanks again Tony!
Bill