private void cmbShape_SelectedIndexChanged(object sender, EventArgs e) { combobox2.SelectedIndex = -1; combobox2.Items.Clear(); string shape = combobox1.Text; IApplication pApplication = ArcMap.Application; IMxDocument pMxDoc = (IMxDocument)pApplication.Document; IActiveView pActiveView = pMxDoc.ActiveView; IMap pMap = pMxDoc.FocusMap; IFeatureLayer pFeatLayer = null; IFeatureClass pFeatClass = null; IFields pFields = null; IField pField; string FieldName; IEnumLayer pEnumLayer = pMap.get_Layers(null, true); pEnumLayer.Reset(); ILayer pLayer = pEnumLayer.Next(); while (pLayer != null) { if (pLayer is ICompositeLayer) { ICompositeLayer pComLayer = (ICompositeLayer)pLayer; int i = pComLayer.Count; for (int j = 0; (j < i); j++) { if (pComLayer.get_Layer(j).Name == shape) { pFeatLayer = (IGeoFeatureLayer)pComLayer.get_Layer(j); } } pFeatClass = pFeatLayer.FeatureClass; pFields = pFeatClass.Fields; int k = pFields.FieldCount; for (int y = 0; (y < k); y++) { pField = pFields.get_Field(y); FieldName = pField.Name; combobox2.Items.Add(FieldName); } } else if(pLayer is IFeatureLayer) { int l = pMap.LayerCount; for (int u = 0; (u < l); u++) { if (pMap.get_Layer(u).Name == shape) { pFeatLayer = (IGeoFeatureLayer)pMap.get_Layer(u); } } pFeatClass = pFeatLayer.FeatureClass; pFields = pFeatClass.Fields; int k = pFields.FieldCount; for (int f = 0; (f < k); f++) { pField = pFields.get_Field(f); FieldName = pField.Name; combobox2.Items.Add(FieldName); } } pLayer = pEnumLayer.Next(); } }
Solved! Go to Solution.
actually posting the error message would be most helpful. This said looking at your code, you don't seem to make a provision for the layer not being found...
Also you seem to be looping through all the layers in the map including the layers inside the composite layers. Then if the layer is composite, you loop through the layers inside that. If the the layer is a featurelayer, you loop again through all the layers on the map again but only the ones at the root level...
I don't understand why the code is so complicated. Based on how you described what you want to do, you can create a .net list of Ilayers from all the layers in the map and bind it to your combobox1 and set the name property as the display property. Then when the selection changes, you can use the selected item from combobox1 which is an iLayer, to look through its fields.
private void cmbShape_SelectedIndexChanged(object sender, EventArgs e) { combobox2.SelectedIndex = -1; combobox2.Items.Clear(); string shape = combobox1.Text; IApplication pApplication = ArcMap.Application; IMxDocument pMxDoc = (IMxDocument)pApplication.Document; IActiveView pActiveView = pMxDoc.ActiveView; IMap pMap = pMxDoc.FocusMap; IFeatureLayer pFeatLayer = null; IFeatureClass pFeatClass = null; IFields pFields = null; IField pField; string FieldName; IEnumLayer pEnumLayer = pMap.get_Layers(null, true); pEnumLayer.Reset(); ILayer pLayer = pEnumLayer.Next(); while (pLayer != null) { string name = pLayer.Name; if (name == shape) { pFeatLayer = (IFeatureLayer)pLayer; pFeatClass = pFeatLayer.FeatureClass; pFields = pFeatClass.Fields; int i = pFields.FieldCount; for (int j = 0; (j < i); j++) { pField = pFields.get_Field(j); FieldName = pField.Name; combobox2.Items.Add(FieldName); } } pLayer = pEnumLayer.Next(); } }