Is there some other way to delete selected layer directory?

10-25-2013 09:08 AM
New Contributor III
Google and do some quick search on esri,I can not find a way to remove the selected layer directory,as far as I know,If I want to remove the selected layer in TOC,I have to search it in TOC and then reflect to MapControl.The code below show how I remove the selected layer.I hope someone can launch some ideal a better or easier way to do this:
/// <summary>
        /// Get selected layer name in TOC.
        /// </summary>
        /// <param name="tocControl"></param>
        /// <returns></returns>
        public static string GetSelectedLayerName(ESRI.ArcGIS.Controls.AxTOCControl tocControl) {
            ESRI.ArcGIS.Controls.esriTOCControlItem item = new ESRI.ArcGIS.Controls.esriTOCControlItem();
            ESRI.ArcGIS.Carto.IBasicMap map = new ESRI.ArcGIS.Carto.MapClass();
            ESRI.ArcGIS.Carto.ILayer layer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
            object legendGroup = new object();
            object index = new object();
            tocControl.GetSelectedItem(ref item, ref map, ref layer, ref legendGroup, ref index);
            if(item == ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemLayer) {
                return layer.Name;
            return null;
/// <summary>
         /// Delete layer.
         /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_deleteLayer_Click(object sender, EventArgs e) {
             string selectedLayer = GetSelectedLayerName();
             for(int i = 0; i < map_main_con.LayerCount; i++) {
                 if(map_main_con.get_Layer(i).Name == selectedLayer) {

Thanks very much.
0 Kudos
0 Replies