Populating a combobox with the fields of a featurelayer nested in a grouplayer

472
2
Jump to solution
10-16-2013 08:31 PM
TauhidulIslam
New Contributor
Hello everyone,
I don't get a clue how to resolve a problem. I have a form which have two combobox and a textbox and a button for search option. when I run the app in combobox1 the LAYER names of TOC will populate, when I select a layer in combobox1 then in combobox2 the fields of that layer will populate. but it is not happening in case of GroupLayer the combobox1 works perfect but combobox2 got messy it populates the field name but populates 6 times and in case of FeatureLayer it give an error.following is my code, Please help me-
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();             }                                  }
0 Kudos
1 Solution

Accepted Solutions
AlexanderGray
Regular Contributor II
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.

View solution in original post

0 Kudos
2 Replies
AlexanderGray
Regular Contributor II
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.

View solution in original post

0 Kudos
TauhidulIslam
New Contributor
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.


thanks for your tips it works well.

now the code is look like this-

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();
            }
        }
0 Kudos