views:

526

answers:

2

Hi I want to know that how can we drag item from treeview in C# and drop in other(autocad) application. That item is basically the autocad file .dwg.

I have written some code:

private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{     

     TreeNode n = (TreeNode)e.Item;
     this.treeView1.DoDragDrop(AcadObj, DragDropEffects.Copy);

}

AcadObj is an autocad object.

This event is working fine but the event DragDrop is not firing, even when i drag on autocad the mouse pointer gives me plus sign (that the file is accepted here). My DragDrop event is :

private void treeview1_DragDrop(object sender, DragEventArgs e){

       MessageBox.Show("It works");
}

The above event is not working and not showing me MessageBox. I have implemented only these two events.

Please helpe me and guide me how can i do this

+1  A: 

The problem is because of memory space. You see, .NET stores its memory inside the CLR. This means , you cannot drag drop anything from .NET into another application running in a different memory space using the .NET dragdrop.

You have to use an interprocess drag-drop.

WINOLEAPI DoDragDrop( 
  IDataObject * pDataObject,  //Pointer to the data object
  IDropSource * pDropSource,  //Pointer to the source
  DWORD dwOKEffect,           //Effects allowed by the source
  DWORD * pdwEffect           //Pointer to effects on the source
);

If you wrap the object you want to drag drop within your own implementation of IDataObject, you can drag drop into any application.

I would post an example, but i cant find one in my source which is "clean" enough to post as an example. Google around. Look for drag-drop implementations using C++ COM. Use that instead of the .NET built in drag drop.

Andrew Keith
+2  A: 

Actually, the OP did not relay all the details. First, he is more than likely already running in the same memory space since 99% of all AutoCAD related programming is done in-process. Second, what version of AutoCAD and what verson of .NET? Without knowing these two key pieces of data, any answer may not be correct.

Having said that, the DragDrop event on the treeview control is never going to fire if you drop into AutoCAD - the event is for dropping on the treeview! When dealing with AutoCAD, calling the DoDrop from treeview is also not the best option. What you should be doing is calling the AutoCAD Application's DoDragDrop event:

AcApp.DoDragDrop(source, data, DragDropEffects.All, New DropTargetNotifier())

The DropTargetNotifier handles the dropped data and would be where you place your messagebox

Mike Tuersley