Reputation:
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
Upvotes: 4
Views: 3614
Reputation: 2193
To make your TreeView control be able to Drag and Drop to AutoCAD drawing area, you will need to host/embed your control in AutoCAD's PaletteSet: Here is my sample (I'm using ListBox here i.e. LB):
public dragdropentity TestLB; //dragdropentity is my actuall control containing my ListBox
[CommandMethod("ListBox")]
public void lb()
{
if (this.TestLB == null)
{
myPaletteSet = new PaletteSet("Test ListBox", new Guid("{B32639EE-05DF-4C48-ABC4-553769C67995}"));
TestLB = new dragdropentity();
myPaletteSet.Add("LB", TestLB);
}
myPaletteSet.Visible = true;
}
Once you are able to display your TreeView in PaletteSet, you can call AutoCAD's application DragDrop method. Here is the code segment from dragdropentity class:
public partial class dragdropentity : UserControl
{
public dragdropentity()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// MessageBox.Show(listBox1.SelectedIndex.ToString() + '\n' + listBox1.SelectedItem.ToString());
pictureBox1.Load(@"D:\My\Documents\Visual Studio 2010\Projects\ClassLibrary1\ClassLibrary1\Images\" + listBox1.SelectedItem.ToString() + ".png");
}
void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int indexOfItem = listBox1.IndexFromPoint(e.X, e.Y);
if (indexOfItem >= 0 && indexOfItem < listBox1.Items.Count) // check that an string is selected
{
listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy);
}
// throw new System.NotImplementedException();
}
void listBox1_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
{
ListBox lb = (ListBox)sender;
textBox1.AppendText('\n' + e.Action.ToString() + '\n'+ this.Name.ToString());
if (e.Action == DragAction.Drop)
{
Autodesk.AutoCAD.ApplicationServices.Application.DoDragDrop(this, "Drag & drop successful!!!", System.Windows.Forms.DragDropEffects.All, new DragDrop());
}
}
}
DragDrop() is your own class handling the Drop event. Here is my code:
class DragDrop : DropTarget
{
public override void OnDrop(System.Windows.Forms.DragEventArgs e)
{
using (DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
// MyCommands mc = new MyCommands();
// mc.CircleJig();
//Call your own methods etc here.
}
}
Upvotes: 0
Reputation: 31
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
Upvotes: 3
Reputation: 7563
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.
Upvotes: 3