[GIS] Counting number of layers selected in ArcMap TOC using ArcObjects

arcmaparcobjects

Has anyone ever tried to get the number of layers that are selected in ArcMap? If I have a map document that has six layers and three of them are selected in the TOC, how does one count them using ArcObjects? I have looked for a while at IContentsView.SelectedItem. Documentation says it can be an enumerator, but I am having a hard time finding out actually what it's type is. In C#, if I do

string myType = IContentsView.SelectedItem.GetType().ToString();

myType is "System._ComObject". Not very descriptive… Trying

if(IContentsView.SelectedItem is IEnumLayer)

and similar checks has been unintuitive… I wasn't sure if multiple layers were selected that they would be stored in there but that was my first guess.

Best Answer

IContentsViewSelection should work for you.

http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//000v00000064000000

This is VB, but you should get the idea...

Dim pCVSel As IContentsViewSelection = My.ArcMap.Document.CurrentContentsView

Then: pCVSel.SelectedItems.Count will give you the count of selected items in the TOC.

Related Question