AnsweredAssumed Answered

Selected Features from multiple layers in table of contents

Question asked by ryan.hawkins on Feb 7, 2013
Latest reply on Feb 8, 2013 by ryan.hawkins
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!

public void StartButton_Click(object sender, EventArgs e)
{
IApplication app = (IApplication)this.Hook;
IMxDocument mapDoc = (IMxDocument)app.Document;
IFeatureLayer featureLayer = (IFeatureLayer)mapDoc.SelectedLayer;
GetSelectedValues(featureLayer);
}

public void GetSelectedValues(IFeatureLayer featureLayer)
{
try
{
IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
ISelectionSet selectionSet = featureSelection.SelectionSet;

ICursor cursor;
selectionSet.Search(null, false, out cursor);
IRow row = cursor.NextRow();

//IFeatureCursor featureCursor = (IFeatureCursor)cursor;
IFeatureClass featureClass = featureLayer.FeatureClass;

// Gets the feature from the OID found from row
IFeature feature = featureClass.GetFeature(row.OID);

while (row != null)
{
feature = featureClass.GetFeature(row.OID);

//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();

//Goes to the NextRow so that the while loop can catch if the next row is a Null.
row = cursor.NextRow();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
}

Outcomes