I have created a form in C# which collects data such as name, date, workspace and several other fields from the user and stores this data in a temporary .txt file to be used later by other functions.

I am trying figure out how to make an ArcMap MXD that when opened will automatically open and run this form and solicit this data from the user.


You should write an ArcMap Addin Extension. In the extension, listen to DocumentOpened event and from there, show your form. Useful Link

To create the extension, use Arcgis visual studio template.

Then change SetupEvents method like this:

private void SetupEvents()
            m_docEvents = ArcMap.Document as IDocumentEvents_Event; 
            m_docEvents.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(OnOpenDocument);

private void OnOpenDocument()
//Show your form here

That works Farid Cher, but another helpful tip is to use the autoLoad property in the Config.esriaddinx:

    <Extension id="Microsoft_OnSave_Extension1" class="Extension1" productName="OnSaveExt" showInExtensionDialog="true" autoLoad="true" />

This may not be there by default but it is safe to add, it forces the tool to execute as ArcMap is initialized (before you even see it). Then in the code use the OnStartup to wire the OpenDocument event of ArcMap events:

//this goes in OnStartup
IDocumentEvents_Event eDocEvents = (IDocumentEvents_Event)ArcMap.Document;
eDocEvents.OpenDocument += new IDocumentEvents_OpenDocumentEventHandler(eDocEvents_OpenDocument);

void eDocEvents_OpenDocument()
    // do your procedure in here

This is the C# translation of working VB code taken from an answer How to trigger a Python script from an ArcGIS Desktop "save" operation? that I have done previously.. I'm hoping nothing has got lost in the translation: the original was VB to be executed in python now translated to C#.

As you can see it's the same (essentially) as the answer by Farid Cher (+1 for that) except that it's not using an extension method, simply an ArcMap Addin, which doesn't need to be installed and can be handled the same as any Esri Addin.

  • 1
    Couldn't you have added this as an edit/comment? – Jason Scheirer May 4 '15 at 23:35
  • Too many words @JasonScheirer. It's almost the same, just slightly different... it's important to know where that property sits in the whole scheme otherwise you could spend frustrating hours trying to find something that's not there. So, you're right, it's not an answer, just a long comment taking the place of an answer. – Michael Stimson May 4 '15 at 23:58
  • Usually I just edit the important missing parts into the already mostly correct answer and people googling for 'the' answer only have to read one answer. It's not worth the imaginary internet points for a 'more right' answer if it doesn't differ much from the other. – Jason Scheirer May 5 '15 at 0:00
  • 1
    Two paths, same destination, it depends on which the reader prefers, there are situations where an addin is not warranted (eg a user can easily turn it off, but an extension method once installed can't be suppressed so easily). That and I really don't like editing other answers except for readability (format code, spelling mistakes) sake; their answer is their I.P. – Michael Stimson May 5 '15 at 0:12
  • It's actually not the author's IP at all – Jason Scheirer May 5 '15 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.