<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Selected Features from multiple layers in table of contents in ArcObjects SDK Questions</title>
    <link>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511035#M13763</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you want the feature selection for all layers in the map then use IMap.FeatureSelection.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 08 Feb 2013 12:33:24 GMT</pubDate>
    <dc:creator>NeilClemmons</dc:creator>
    <dc:date>2013-02-08T12:33:24Z</dc:date>
    <item>
      <title>Selected Features from multiple layers in table of contents</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511033#M13761</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am able to get the ObjectID for the features only in the layer selected in the table of contents when I click the StartButton. I would like to be able to gather the selected items for multiple layers. This is because there are multiple layers that have selected features and need to grab information for each. Below I have added the code I used to put the ObjectID into 3 comboboxes. I realize the line IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer; is selecting the layer in the table of contents that is selected. I am wondering how others would solve this. I have tried to find a way to use code to select a different layer by its layer name but I could not find any information on it. If I can select a layer by name then I can merely change the layer before the IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer; line and run the method GetSelectedValues for each layer I would like. Any suggestions will be greatly appreciated! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; public void StartButton_Click(object sender, EventArgs e)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; {&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IApplication app = (IApplication)this.Hook;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IMxDocument mapDoc = (IMxDocument)app.Document;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; GetSelectedValues(featureLayer);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; public void GetSelectedValues(IFeatureLayer featureLayer)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; {&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; try&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; {&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; ISelectionSet selectionSet = featureSelection.SelectionSet;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; ICursor cursor;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; selectionSet.Search(null, false, out cursor);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IRow row = cursor.NextRow();&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; //IFeatureCursor featureCursor = (IFeatureCursor)cursor;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IFeatureClass featureClass = featureLayer.FeatureClass;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; // Gets the feature from the OID found from row&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; IFeature feature = featureClass.GetFeature(row.OID);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; while (row != null)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; { &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; feature = featureClass.GetFeature(row.OID);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; //Adds the selected object ID's to the drop down menus&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; middlePole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; leftPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; rightPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString());&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; //textBox3.Text = feature.get_Value(featureClass.FindField("LASTUPDATESESSIONID")).ToString();&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; //Goes to the NextRow so that the while loop can catch if the next row is a Null.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; row = cursor.NextRow();&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; catch (Exception ex)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; {&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; MessageBox.Show(ex.StackTrace);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; }&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; }&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Feb 2013 20:28:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511033#M13761</guid>
      <dc:creator>RyanHawkins</dc:creator>
      <dc:date>2013-02-07T20:28:55Z</dc:date>
    </item>
    <item>
      <title>Re: Selected Features from multiple layers in table of contents</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511034#M13762</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Please use CODE tags!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Firts of all:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;while (row != null) { feature = featureClass.GetFeature(row.OID);&amp;nbsp; //Adds the selected object ID's to the drop down menus middlePole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); leftPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); rightPole.Items.Add(feature.get_Value(featureClass.FindField("OBJECTID")).ToString()); //textBox3.Text = feature.get_Value(featureClass.FindField("LASTUPDATESESSIONID")).ToString();&amp;nbsp; //Goes to the NextRow so that the while loop can catch if the next row is a Null. row = cursor.NextRow(); }&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Isn't it simpler this way:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;{&amp;nbsp; while(row != null)&amp;nbsp; { &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string objectID = GetStringValueFromField(row, "OBJECTID"); &amp;nbsp; middlePole.Items.Add(objectID); &amp;nbsp; leftPole.Items.Add(objectID); &amp;nbsp; rightPole.Items.Add(objectID); &amp;nbsp; row = cursor.NextRow();&amp;nbsp; } } private string GetStringValueFromField(IRow row, string fieldName) {&amp;nbsp; int index = row.Fields.FindField(fieldName);&amp;nbsp; string fieldValue;&amp;nbsp; if (index &amp;gt; -1)&amp;nbsp; { &amp;nbsp; fieldValue = row.get_Value(index).ToString(); &amp;nbsp; return fieldValue;&amp;nbsp; }&amp;nbsp; else &amp;nbsp; return fieldValue = ""; }&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;By the way you don't have to get feature to get value from field(as you can see in GetStringValueFromField method).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To relate to topic of your thread. Enumerate through the layers in the TOC, example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;IUID uid = new UIDClass(); uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}"; IEnumLayer enumLayer = map.get_Layers((UID)uid, true); enumLayer.Reset(); IFeatureLayer featLayer = (IFeatureLayer)enumLayer.Next(); while(featLayer != null) {&amp;nbsp; //your logic, for example&amp;nbsp; IFeatureSelection featureSelection = (IFeatureSelection)featLayer;&amp;nbsp; ICursor cursor;&amp;nbsp; selectionSet.Search(null, false, out cursor);&amp;nbsp; IRow row = cursor.NextRow();&amp;nbsp; ...etc &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //////////////&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; featLayer = (IFeatureLayer)enumLayer.Next(); }&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;MDruzgala&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Feb 2013 06:24:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511034#M13762</guid>
      <dc:creator>MarcinDruzgala</dc:creator>
      <dc:date>2013-02-08T06:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Selected Features from multiple layers in table of contents</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511035#M13763</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you want the feature selection for all layers in the map then use IMap.FeatureSelection.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Feb 2013 12:33:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511035#M13763</guid>
      <dc:creator>NeilClemmons</dc:creator>
      <dc:date>2013-02-08T12:33:24Z</dc:date>
    </item>
    <item>
      <title>Re: Selected Features from multiple layers in table of contents</title>
      <link>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511036#M13764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you both for the responses! I really appreciate critiquing my code, I am learning c# on the fly and am missing some basic concepts so I really appreciate that. The code you provided has allowed me to do exactly what I asked!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 08 Feb 2013 16:21:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcobjects-sdk-questions/selected-features-from-multiple-layers-in-table-of/m-p/511036#M13764</guid>
      <dc:creator>RyanHawkins</dc:creator>
      <dc:date>2013-02-08T16:21:26Z</dc:date>
    </item>
  </channel>
</rss>

