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.
-
1There's an example here edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriArcMapUI/… it should be IEnumObject edndoc.esri.com/arcobjects/9.2/ComponentHelp/esriGeoDatabase/…Michael Stimson– Michael Stimson2015年03月05日 02:50:29 +00:00Commented Mar 5, 2015 at 2:50
-
Thanks, Michael. I think I saw something similar to this but I will give it another look when I have my code in front of me. I didn't remember seeing an IEnumObjects. Hopefully it is still there at 10.0.dlbran– dlbran2015年03月05日 03:29:45 +00:00Commented Mar 5, 2015 at 3:29
-
help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/… AND resources.arcgis.com/en/help/arcobjects-net/componenthelp/…Michael Stimson– Michael Stimson2015年03月05日 03:48:58 +00:00Commented Mar 5, 2015 at 3:48
1 Answer 1
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.
-
Thanks, @Dan. This is probably cleaner than what I ended up doing. I looked at link and finally realized that SelectedItem is an ISet when it contains more than one object. I did the QI on that rather than on IContentsView. I will give you the answer though since you are correct. Thanks for your input!dlbran– dlbran2015年03月06日 02:43:55 +00:00Commented Mar 6, 2015 at 2:43