Sunday, 6 September 2015

arcobjects - Event raised when selected layer changes in TOC


Is there an interface which exposes events for when a TOC selected item changes?


I can find snippets showing how to get the currently selected feature from the TOC. However, I have not been able to find anything that allows you to track when a selection changes - only query the current state.


What would be the best approach (preferably other than polling) to track the user's current layer selected within ArcMap and ArcScene?



Answer




There is indeed no appropriate event. As described in links in another post, people have worked around this by polling for the state in a command's Enabled() method, which gets called very often by the framework. But this of course assumes you do have a command and fails when the command is not on any toolbars or top-level menus, in which case Enabled() does not get called or gets called only on demand.


In the past, I remember "solving" this problem by hooking into the display contents view (i.e. TOC) window procedure, intercepting mouse and keyboard messages and tracking if the selection changed. Terrible hack, but worked. If you're interested, I'll port it to .NET and post here, but as I said and will say again, it is a very ugly workaround.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...