RemoveShape.cs

This example demonstrates how to remove shapes by mouse click and update their labels. Here is a screenshot with the results of the code.

using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

public partial class MapExamples
{
    // <summary>
    // Loads the layers and registers event handler
    // </summary>
    public void RemoveShape(AxMap axMap1, string dataPath)
    {
        string filename = dataPath + "natural.shp";

        if (!File.Exists(filename))
        {
            MessageBox.Show("Couldn't file the file: " + filename);
            return;
        }

        Shapefile sf = new Shapefile();
        sf.Open(filename, null);
        if (!sf.StartEditingShapes(true, null))
        {
            MessageBox.Show("Failed to start edit mode: " + sf.get_ErrorMsg(sf.LastErrorCode));
        }
        else
        {
            int fieldIndex = sf.EditAddField("ShapeIndex", FieldType.INTEGER_FIELD, 0, 0);
            for (int i = 0; i < sf.NumShapes; i++)
                sf.EditCellValue(fieldIndex, i, i);
            sf.Labels.Generate("[ShapeIndex]", tkLabelPositioning.lpCentroid, false);
            sf.Labels.Synchronized = true;

            axMap1.AddLayer(sf, true);
            axMap1.SendMouseDown = true;
            MapEvents.MouseDownEvent += new AxMapWinGIS._DMapEvents_MouseDownEventHandler(axMap1_MouseDownEvent1);  // change MapEvents to axMap1
            axMap1.MapUnits = tkUnitsOfMeasure.umMeters;
            axMap1.CurrentScale = 50000;
            axMap1.CursorMode = tkCursorMode.cmNone;
        }
    }

    private void axMap1_MouseDownEvent1(object sender, _DMapEvents_MouseDownEvent e)
    {
        // it's assumed here that the layer we want to edit is the first 1 (with 0 index)
        int layerHandle = axMap1.get_LayerHandle(0);  
        Shapefile sf = axMap1.get_Shapefile(layerHandle);
        if (sf != null)
        {
            double projX = 0.0;
            double projY = 0.0;
            axMap1.PixelToProj(e.x, e.y, ref projX, ref projY);
            
            object result = null;
            Extents ext = new Extents();
            ext.SetBounds(projX, projY, 0.0, projX, projY, 0.0);
            if (sf.SelectShapes(ext, 0.0, SelectMode.INCLUSION, ref result))
            {
                int[] shapes = result as int[];
                if (shapes.Length > 1)
                {
                    string s = "More than one shapes were selected. Shape indices:";
                    for (int i = 0; i < shapes.Length; i++)
                        s += shapes[i].ToString() + Environment.NewLine;
                    MessageBox.Show(s);
                }
                else
                {
                    if (!sf.EditDeleteShape(shapes[0]))
                    {
                        MessageBox.Show("Failed to delete a shape: " + sf.get_ErrorMsg(sf.LastErrorCode));
                    }
                    else
                    {
                        //MessageBox.Show("Shape was removed. Index = " + shapes[0].ToString());
                        sf.Labels.Expression = sf.Labels.Expression;
                        for (int i = 0; i < sf.Labels.Count; i++)
                            sf.Labels.get_Label(i, 0).Text += "; " + i.ToString();
                        axMap1.Redraw();
                    }
                }
            }
            else
            {
                MessageBox.Show("Nothing was selected");
            }
        }

        // Execute this code if you want to save the results.
        // sf.StopEditingShapes(true, true, null);
    }
}
 All Classes Files Functions Enumerations Properties