Drag & Drop an Item into ArcMap

2160
2
Jump to solution
02-27-2013 10:13 PM
MickeyO_Neil
New Contributor III
Hello,

I am looking for the right Object Typ to put in the DoDragDrop() Function.
I tried it with an IName Object and an IDataObject but ArcMap does not accept these Typs.
The Drag&Drop even works with the Windows Explorer so it musst be a common Object Typ. I bet this is somewhere in the "ArcObjects Help" but I can't find it, sorry. 🙂

My Item schould be draged out of a TreeView into ArcMap. Thats my Code:

Private Sub tvTree_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles tvTree.ItemDrag          'Get the IName Object of the TreeNode         Dim pTreeNode As TreeNode         pTreeNode = e.Item          Dim pclsTreeNode As clsTreeNode         pclsTreeNode = pTreeNode.Tag          Dim pName As IName         pName = pclsTreeNode.NodeObject          If Not pName Is Nothing Then              'Create IData Object              Dim pDataObject As Windows.Forms.IDataObject             pDataObject = New Windows.Forms.DataObject             pDataObject.SetData(pName)              DoDragDrop(pDataObject, DragDropEffects.All)         End If     End Sub
0 Kudos
1 Solution

Accepted Solutions
MickeyO_Neil
New Contributor III
It finally works 🙂
You need a MemoryStream to set Data into a DataObject 😛

  Private Sub tvTree_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles tvTree.ItemDrag         Try              'Get the IName Object (I stored it in the Tag of the TreeNode)             Dim pTreeNode As TreeNode             pTreeNode = e.Item              'My Class where I stored the IName and some other stuff             Dim pclsTreeNode As clsTreeNode             pclsTreeNode = pTreeNode.Tag              Dim pName As IName             pName = pclsTreeNode.NodeObject              If Not pName Is Nothing Then                 '----- For ArcMap -----                  'Put the IName in an IEnumName                 Dim pNameFactory As INameFactory                 pNameFactory = New NameFactory                  Dim pEnumName As IEnumName                 pEnumName = New NamesEnumerator                 Dim pEnumNameEdit As IEnumNameEdit                 pEnumNameEdit = pEnumName                 pEnumNameEdit.Add(pName)                  'Create DataObject                 Dim pDataObject As Windows.Forms.IDataObject                 pDataObject = New Windows.Forms.DataObject                   'To fill the DataObject you need a MemoryStream!                 Dim pMemoryStream As System.IO.MemoryStream                 pMemoryStream = New System.IO.MemoryStream(CType(pNameFactory.PackageNames(pEnumNameEdit), Byte()))                 pDataObject.SetData("ESRI Names", pMemoryStream)                  'Fire the DragGropEvent                 tvTree.DoDragDrop(pDataObject, DragDropEffects.All)             Else                 '----- For Windows Explorer -----                  'For the Internet Explorer you just need the Path of the File and put it into an Array of String                 Dim pArrayOfString(0) As String                 pArrayOfString(0) = pclsTreeNode.Path                  Dim pDataObject As Windows.Forms.IDataObject                 pDataObject = New Windows.Forms.DataObject                  pDataObject.SetData(DataFormats.FileDrop, pArrayOfString)                 tvTree.DoDragDrop(pDataObject, DragDropEffects.All)             End If         Catch ex As Exception             DoErrorHandling(ex, Reflection.MethodInfo.GetCurrentMethod)         End Try     End Sub

View solution in original post

0 Kudos
2 Replies
MickeyO_Neil
New Contributor III
Hi,

...I figured out, that the Object has to be an Array with the Path of the draged file or an IEnumName Object.
Because that's the code which acceps the Drag Event:

 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


But it still does not work. Maybe the "DoDragDrop" Function is a wrong choice to fire the "AxMapControl1_OnOleDrop" Event.
0 Kudos
MickeyO_Neil
New Contributor III
It finally works 🙂
You need a MemoryStream to set Data into a DataObject 😛

  Private Sub tvTree_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles tvTree.ItemDrag         Try              'Get the IName Object (I stored it in the Tag of the TreeNode)             Dim pTreeNode As TreeNode             pTreeNode = e.Item              'My Class where I stored the IName and some other stuff             Dim pclsTreeNode As clsTreeNode             pclsTreeNode = pTreeNode.Tag              Dim pName As IName             pName = pclsTreeNode.NodeObject              If Not pName Is Nothing Then                 '----- For ArcMap -----                  'Put the IName in an IEnumName                 Dim pNameFactory As INameFactory                 pNameFactory = New NameFactory                  Dim pEnumName As IEnumName                 pEnumName = New NamesEnumerator                 Dim pEnumNameEdit As IEnumNameEdit                 pEnumNameEdit = pEnumName                 pEnumNameEdit.Add(pName)                  'Create DataObject                 Dim pDataObject As Windows.Forms.IDataObject                 pDataObject = New Windows.Forms.DataObject                   'To fill the DataObject you need a MemoryStream!                 Dim pMemoryStream As System.IO.MemoryStream                 pMemoryStream = New System.IO.MemoryStream(CType(pNameFactory.PackageNames(pEnumNameEdit), Byte()))                 pDataObject.SetData("ESRI Names", pMemoryStream)                  'Fire the DragGropEvent                 tvTree.DoDragDrop(pDataObject, DragDropEffects.All)             Else                 '----- For Windows Explorer -----                  'For the Internet Explorer you just need the Path of the File and put it into an Array of String                 Dim pArrayOfString(0) As String                 pArrayOfString(0) = pclsTreeNode.Path                  Dim pDataObject As Windows.Forms.IDataObject                 pDataObject = New Windows.Forms.DataObject                  pDataObject.SetData(DataFormats.FileDrop, pArrayOfString)                 tvTree.DoDragDrop(pDataObject, DragDropEffects.All)             End If         Catch ex As Exception             DoErrorHandling(ex, Reflection.MethodInfo.GetCurrentMethod)         End Try     End Sub
0 Kudos