Select to view content in your preferred language

IContentsView SelectedItem Enumerator

642
3
11-08-2011 03:40 PM
HenryColgate
Regular Contributor
I'm trying to get a list of Layers selected in the Table of Contents.  That is they are highlighted whether they are checked on or off in the tick box.

When I get the selected item and a single layer is selected in the ToC  the returned object is an ILayer.  However if more than one layer is selected it is not an IEnumLayer which I thought was logical and it is not a System.Array which seemed to be alluded to but not stated specifically.

It is stated that the return value could be an enum but I don't know which Enum type to cast to.

In the code below if multiple layers are selected in the ToC what is the type of object x?


IMxDocument mxDocument = (IMxDocument)ChangeMultipleSymbology_AddIn.ArcMap.Document;
IActiveView activeView = (IActiveView)mxDocument.ActiveView;
IContentsView contentsView = (IContentsView)mxDocument.CurrentContentsView;
        
System.Object x = contentsView.SelectedItem;



If there is a better way to get a list of values that are selected in the ToC I am open to suggestions.
0 Kudos
3 Replies
HenryColgate
Regular Contributor
Answering my own question after searching the old forum again. Eventually I will port all of that knowledge over here and then ESRI can create a new forum so we have three to search.

The answer is it is an ESRI.ArcGIS.esriSystem.ISet not an enumerator that is named as such.

Let's make the old forum searchable from here.
0 Kudos
KevinOrcutt
Emerging Contributor
Could you elaborate a bit more???   I'm am trying to either determine the number of "selected" layers or clear them...  It sounds like what you found out will help me out some...
Thanks in advance,
Kevin Orcutt
0 Kudos
HenryColgate
Regular Contributor
Okay so the code above works to get an System.Object called 'x' right but then I didn't know what to do because it should have been IEnum or some such according to the documentation but it is type ISet.

So using the code I wrote before first test to see whether it is of type ISet.  This is because other selected features can be included which makes it something else, can't recall off the top of my head what these other selected features might be but remember having the problem. 

If it is of type ISet then it is probably just the selected ToC layers, which is what you want right?, though I have not looked into it to much so if it falls over it could be something I never encountered.

so some simple code is (in C#)

System.Object x = contentsView.SelectedItem

if ((x is ESRI.ArcGIS.esriSystem.ISet))
{
    ISet set = (ISet) x;
    int numSelectedLyr = set.Count

    ...blah blah whatever code you want....

}


I think this may get a bit tricky with Group Layers also but haven't tried that so keep it in mind.

Hope that helps.  If you're still confused post again and I will try to help you out with some better code or maybe a better explanation dependging on what you want.
0 Kudos