This document is archived and information here might be outdated.  Recommended version.


How to drop data onto the MapControl (ArcObjects .NET 10.5 SDK)

How to drop data onto the MapControl


Summary
This topic describes how to enable data to be dragged from ArcCatalog and Windows Explorer, and dropped onto the MapControl. It describes handling the drop event and creating the new layers.

Dropping data onto the MapControl

Do the following steps to drop data onto the MapControl:
  1. Set the IMapControl4.OleDropEnabled property to true.
  2. Set the IMapControlEvents2.OnOleDrop event to axMapControl1_OnOleDrop.
  3. Manage the esriDropEnter action of the onOLEDrop event:
    • When dragged, data initially enters the MapControl and the OnOleDrop event is triggered with an esriDropEnter action. The IDataObjectHelper interface is used to determine whether the data is being dragged from ArcCatalog or Windows Explorer using the CanGetNames and CanGetFiles methods. If the data is from ArcCatalog or Windows Explorer, the DragDropEffect is set to esriDragDropCopy; otherwise, it is set to esriDragDropNone. The DragDropEffect is then stored for later use, for example, when the same data is dragged around or dropped onto the MapControl.
  4. Manage the esriDropOver action of the onOLEDrop event:
    • When data is being dragged around the MapControl, the OnOleDrop event is triggered with an esriDropOver action. The DragDropEffect is set to the effect stored when the data initially entered the control.
  5. Manage the esriDropped action of the onOLEDrop event:
    • When data is dropped onto the MapControl, the OnOleDrop event is triggered with an esriDropped action. The IDataObjectHelper interface is used to obtain an array of file path strings from the data using the GetFiles method if the CanGetFiles method returns true; otherwise, an IEnumName collection is obtained using the GetNames method. The CheckMxFile method is used to determine whether each file path in the array is a valid Mx document. If valid, the LoadMxFile method is used to load the document into the MapControl; otherwise, a new IFileName object is created with its path property set to the file path in the array. IFileName is used to create a layer that can be added to the MapControl in the same way that the INames obtained by enumerating the IEnumName collection do.
If dropped from ArcCatalog, Mx document and layer (.lyr) files will return true for CanGetFiles as they are both a type of IFileName.
  1. Call the CreateLayersFromName method to create a collection of ILayers by using the ILayerFactoryHelper interface. Each layer is then added to the MapControl using the AddLayer method.
Copy and paste the following axMapControl1_OnOleDrop method (for Steps 3-5) and the CreateLayer method (for Step 6) into your application where appropriate:
[C#]
private void axMapControl1_OnOleDrop(object sender,
    ESRI.ArcGIS.Controls.IMapControlEvents2_OnOleDropEvent e)
{
    //QI for IDataObjectHelper.
    IDataObjectHelper dataObject=(IDataObjectHelper)e.dataObjectHelper;
    esriControlsDropAction action=e.dropAction;
    e.effect=(int)esriControlsDragDropEffect.esriDragDropNone;
    //If the mouse has just entered the MapControl.
    if (action == esriControlsDropAction.esriDropEnter)
    {
        //If the data is from Windows Explorer or ArcCatalog.
        if (dataObject.CanGetFiles() | dataObject.CanGetNames())
        {
            //Store the copy effect.
            m_Effect=esriControlsDragDropEffect.esriDragDropCopy;
        }
    }
    //If the mouse is moving over the MapControl.
    if (action == esriControlsDropAction.esriDropOver)
    {
        //Set the effect.
        e.effect=(int)m_Effect;
    }
    //If data is dropped onto the MapControl.
    if (action == esriControlsDropAction.esriDropped)
    {
        //If the data is from Windows Explorer.
        if (dataObject.CanGetFiles() == true)
        {
            //Get an array of file paths through the IDataObjectHelper.
            System.Array filePaths=System.Array.CreateInstance(typeof(string), 0,
                0);
            filePaths=(System.Array)dataObject.GetFiles();
            //Loop through the array.
            for (int i=0; i <= filePaths.Length - 1; i++)
            {
                //If a valid Mx document, load it into the MapControl.
                if (axMapControl1.CheckMxFile(filePaths.GetValue(i).ToString()) ==
                    true)
                {
                    try
                    {
                        axMapControl1.LoadMxFile(filePaths.GetValue(i).ToString(),
                            Type.Missing, "");
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("Error:" + ex.Message);
                        return ;
                    }
                }
                else
                {
                    //Get the IFileName interface and set its path.
                    IFileName fileName=new FileNameClass();
                    fileName.Path=filePaths.GetValue(i).ToString();
                    //Create a map layer.
                    CreateLayer((IName)fileName);
                }
            }
        }
        //If data is from ArcCatalog.
        else if (dataObject.CanGetNames() == true)
        {
            //Get the IEnumName interface through the IDataObjectHelper.
            IEnumName enumName=dataObject.GetNames();
            enumName.Reset();
            //Get the IName interface.
            IName name=enumName.Next();
            //Loop through the names.
            while (name != null)
            {
                //Create a map layer.
                CreateLayer(name);
                name=enumName.Next();
            }
        }
    }
}

//Create a layer or layers from a name and add to MapControl.
private void CreateLayer(IName name)
{
    //Set the mouse pointer.
    axMapControl1.MousePointer=esriControlsMousePointer.esriPointerHourglass;
    //Get the ILayerFactoryHelper interface.
    ILayerFactoryHelper layerFactoryHelper=new LayerFactoryHelperClass();
    //Get the IEnumLayer interface through the ILayerFatcoryHelper interface.
    try
    {
        IEnumLayer enumLayer=layerFactoryHelper.CreateLayersFromName(name);
        enumLayer.Reset();
        //Get the ILayer interface.
        ILayer layer=enumLayer.Next();
        //Loop through layers.
        while (layer != null)
        {
            //Add the layer to the map.
            axMapControl1.AddLayer(layer, 0);
            layer=enumLayer.Next();
        }
    }
    catch (System.Exception ex)
    {
        MessageBox.Show("Error:" + ex.Message);
    }
    //Set the mouse pointer.
    axMapControl1.MousePointer=esriControlsMousePointer.esriPointerDefault;
}
[VB.NET]
Private Sub AxMapControl1_OnOleDrop(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnOleDropEvent) Handles AxMapControl1.OnOleDrop
    On Error GoTo ErrorHandler
    'QI for IDataObjectHelper.
    Dim pDataObject As IDataObjectHelper
    pDataObject=e.dataObjectHelper
    Dim action As esriControlsDropAction
    action=e.dropAction
    e.effect=esriControlsDragDropEffect.esriDragDropNone
    'If the mouse has entered the MapControl.
    If action=esriControlsDropAction.esriDropEnter Then
        'If the data is from Windows Explorer or ArcCatalog.
        If pDataObject.CanGetFiles Or pDataObject.CanGetNames Then
            'Store the copy effect.
            m_pEffect=esriControlsDragDropEffect.esriDragDropCopy
        End If
    End If
    'If the mouse is moving over the MapControl.
    If action=esriControlsDropAction.esriDropOver Then
        'Set the effect.
        e.effect=m_pEffect
    End If
    'If data is dropped onto the MapControl.
    Dim pFilePaths As Object
    Dim i As Short
    Dim pFileName As IFileName
    Dim pEnumName As IEnumName
    Dim pName As IName
    If action=esriControlsDropAction.esriDropped Then
        'If the data is from Windows Explorer.
        If pDataObject.CanGetFiles Then
            'Get an array of file paths through the IDataObjectHelper.
            pFilePaths=pDataObject.GetFiles
            'Loop through the array.
            For i=LBound(pFilePaths) To UBound(pFilePaths)
                'If a valid Mx document, load it into the MapControl.
                If AxMapControl1.CheckMxFile(pFilePaths(i)) Then
                    AxMapControl1.LoadMxFile(pFilePaths(i))
                Else
                    'Get the IFileName interface and set its path.
                    pFileName=New FileNameClass
                    pFileName.Path=pFilePaths(i)
                    'Create a map layer.
                    CreateLayer(CType(pFileName, IName))
                End If
            Next
            'If data is from ArcCatalog.
        ElseIf pDataObject.CanGetNames Then
            'Get the IEnumName interface through the IDataObjectHelper.
            pEnumName=pDataObject.GetNames
            pEnumName.Reset()
            'Get the IName interface.
            pName=pEnumName.Next
            'Loop through the names.
            Do While Not pName Is Nothing
                'Create a map layer.
                CreateLayer(pName)
                pName=pEnumName.Next
            Loop
        End If
    End If
    Exit Sub
ErrorHandler:
    Select Case Err.Number
        Case 1031
            MsgBox("Unable to load password protected ArcReader files (*.pmf)")
            Exit Sub
    End Select
End Sub

'Create a layer or layers from a name and add to MapControl.

Public Sub CreateLayer(ByRef pName As IName)
    On Error GoTo ErrorHandler
    'Set the mouse pointer.
    AxMapControl1.MousePointer=esriControlsMousePointer.esriPointerHourglass
    'Get the ILayerFactoryHelper interface.
    Dim pLayerFactoryHelper As ILayerFactoryHelper
    pLayerFactoryHelper=New LayerFactoryHelperClass
    'Get the IEnumLayer interface through the ILayerFatcoryHelper interface.
    Dim pEnumLayer As IEnumLayer
    pEnumLayer=pLayerFactoryHelper.CreateLayersFromName(pName)
    pEnumLayer.Reset()
    'Get the ILayer interface.
    Dim pLayer As ILayer
    pLayer=pEnumLayer.Next
    'Loop through layers.
    Do While Not pLayer Is Nothing
        'Add the layer to the map.
        AxMapControl1.AddLayer(pLayer)
        pLayer=pEnumLayer.Next
    Loop
    'Set the mouse pointer.
    AxMapControl1.MousePointer=esriControlsMousePointer.esriPointerDefault
    Exit Sub
ErrorHandler:
    Select Case Err.Number
        Case 1028
            MsgBox("Data not supported")
            AxMapControl1.MousePointer=esriControlsMousePointer.esriPointerDefault
    End Select
End Sub


See Also:

MapControl class
IMapControl4 interface




Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced