kyanuk1

Loading features from ArcMap for use

Discussion created by kyanuk1 on Aug 10, 2012
I have two questions regarding loading the layers currently within an ArcMap Document.


Question 1:

As I have it now, I just have a button_onclick method which is a "Refresh Layers" button:

private List<IFeatureLayer> LoadFeatureLayersFromMap()
        {
            try
            {
                List<IFeatureLayer> fList = new List<IFeatureLayer>();
                UID lyrUID = new ESRI.ArcGIS.esriSystem.UIDClass();
                lyrUID.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
                IEnumLayer eLayer = mxd.get_Layers(lyrUID, true);
                ILayer lyr = eLayer.Next();
                while (lyr != null)
                {
                    IFeatureLayer fl = (IFeatureLayer)lyr;
                    if (fl != null) if (fl.FeatureClass != null) fList.Add(fl);
                    lyr = eLayer.Next();
                }
                return fList;
            }
            catch (Exception)
            {
                return null;
            }
        }



This works fine, as I use the list as a data source for comboboxes, listboxes, etc.

Is there a nicer way to load features automatically without having to provide a "Refresh" button every time the user wants to use the tool?



Question 2:

Using the described method above, the refresh method is added to my DockableWindow class for each tool I write.
Instead of having to use this code over and over, I was making a new "MapDataHandler" class that uses these data handling methods, but I encountered something interesting... If the map is saved with the tool open, and I try to use the refresh layers from my new MapDataHandler class, the operation does not function.  However, if the map is saved with the tool not open, and the user clicks the button to open it, the button works as intended...

This does not occur if I use the LoadFeaturesFromMap method directly within my DockableWindow class.


I use the code as follows:

class MapDataHandler
    {
        
        IMap mxd;
        public MapDataHandler(IMap inmap)
        {
            mxd = inmap;
        }

        
        public List<IFeatureLayer> LoadFeatureLayersFromMap()
        {
            try
            {
                List<IFeatureLayer> fList = new List<IFeatureLayer>();
                UID lyrUID = new ESRI.ArcGIS.esriSystem.UIDClass();
                lyrUID.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
                IEnumLayer eLayer = mxd.get_Layers(lyrUID, true);
                ILayer lyr = eLayer.Next();
                while (lyr != null)
                {
                    IFeatureLayer fl = (IFeatureLayer)lyr;
                    if (fl != null) if (fl.FeatureClass != null) fList.Add(fl);
                    lyr = eLayer.Next();
                }
                return fList;
            }
            catch (Exception)
            {
                return null;
            }
        }



In the DockableWindow class:
MapDataHandler mdh = new MapDataHandler(ArcMap.Document.FocusMap);

 private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.DataSource = null;
            
            comboBox1.DataSource = mdh.LoadFeatureLayersFromMap();
            comboBox1.DisplayMember = "Name";

            comboBox1.Refresh();
        }

Outcomes