HOW TO

Drag and drop one or more layers to a Map using C#

Last Published: April 25, 2020

Summary

The C# sample code below demonstrates how to add one or more layers to a Map by dragging shapefiles from Windows Explorer and dropping them onto the Map.

Procedure

The instructions below assume a C# Windows Application project has been created with a Map control added to the form and event procedures for DragFiles and DropFiles.
  1. Paste the following code into the DragFiles event procedure.
    try
    {
    MapObjects2.Strings str =
    (MapObjects2.Strings) e.fileNames;
    if (str.Count > 0)
    {
    e.dropValid = true;
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

  2. Paste the following code into the DropFiles event procedure.

    try
    {
    MapObjects2.Strings str =(MapObjects2.Strings) e.fileNames;
    if (str.Count > 0)
    {
    e.dropValid = true;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }

    }

    private void axMap1_DropFiles(object sender, AxMapObjects2._DMapEvents_DropFilesEvent e)
    {
    try
    {
    DataConnection dc = new DataConnectionClass();
    string shp;
    string shpPath;
    MapLayer ml;
    ArrayList ptAl = new ArrayList();
    ArrayList lineAL = new ArrayList();
    ArrayList polyAL = new ArrayList();
    ArrayList imgAL = new ArrayList();

    MapObjects2.Strings str = (MapObjects2.Strings) e.fileNames;
    shpPath = Path.GetDirectoryName(str.Item(0));
    dc.Database = shpPath;
    if (dc.Connect())
    {
    //Add dropped layers to Map
    for(int i = 0; i < str.Count; i ++)
    {
    ml = new MapLayerClass();
    shp = Path.GetFileNameWithoutExtension(str.Item(i));
    ml.GeoDataset = dc.FindGeoDataset(shp);
    axMap1.Layers.Add (ml);
    }
    //Sort layers

    for (int j=0; j < axMap1.Layers.Count; j++)
    {
    //Check if it's a map layer or image layer
    MapLayer lt =(MapLayer) axMap1.Layers.Item(j);
    if (lt.LayerType == MapObjects2.LayerTypeConstants.moMapLayer)
    {
    switch (lt.shapeType)
    {
    case MapObjects2.ShapeTypeConstants.moShapeTypePoint:
    ptAl.Add (axMap1.Layers.Item(j));
    break;
    case MapObjects2.ShapeTypeConstants.moShapeTypeLine:
    lineAL.Add (axMap1.Layers.Item(j));
    break;
    case MapObjects2.ShapeTypeConstants.moShapeTypePolygon:
    polyAL.Add (axMap1.Layers.Item(j));
    break;
    }
    }
    else
    {
    imgAL.Add (axMap1.Layers.Item(j));
    }
    }
    }
    axMap1.Layers.Clear();
    //Add the layers to the map sorted by type
    for (int k=0;k<imgAL.Count; k ++)
    {
    axMap1.Layers.Add (imgAL[k]);
    }
    for (int k = 0; k < polyAL.Count; k ++)
    {
    axMap1.Layers.Add(polyAL[k]);
    }
    for (int k = 0;k<lineAL.Count;k++)
    {
    axMap1.Layers.Add(lineAL[k]);
    }
    for (int k=0; k< polyAL.Count; k++)
    {
    axMap1.Layers.Add(polyAL[k]);
    }
    axMap1.Extent = axMap1.FullExtent;
    axMap1.Refresh();

    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message.ToString());
    }

Article ID:000006142

Software:
  • Legacy Products

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic