怎么做拖放 [英] How to do drag drop
问题描述
How to do drag drop from one windows form to another windows form ?
我尝试过:
我试图使用这里的例子
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=vs .110).aspx
但我想要的是数据是从另一个窗口形式拖动到另一个窗口形式。
What I have tried:
I have tried to used the example like here
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=vs.110).aspx
but what I want is the data is drag from another window form to another window form.
推荐答案
这取决于你拖动的内容,但是......
这是一个两阶段的过程:你需要在目标表单中启用drop(这使用文件)
1)将表单的AllowDrop属性设置为True
2)为表单的DragEnter和DragDrop事件创建一个处理程序。
3)在DragEnter处理程序,添加以下代码行:
It depends what you are dragging, but...
It's a two stage process: you need to enable drop in the destination form (this uses files)
1) Set the AllowDrop property of your form to True
2) Create a handler for both the DragEnter and DragDrop events of your form.
3) In the DragEnter handler, add the following line of code:
e.Effect = DragDropEffects.Move;
4)在DragDrop处理程序中,添加以下内容:
4) In the DragDrop handler, add the following:
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (files != null)
{
foreach (string file in files)
{
Console.WriteLine(file); // Or whatever you need to do...
}
}
在源表格中,它有点复杂,因为您需要使用支持拖动的控件,例如ListView。
1)处理控件ItemDrag事件。在这种情况下,ListView:
In your source form, it's a little more complex, as you need to use a control that supports dragging, such as the ListView.
1) Handle the controls ItemDrag event. In this case, a ListView:
private void myListView_ItemDrag(object sender, ItemDragEventArgs e)
{
StringCollection s = new StringCollection();
s.Add(@"D:\Test Data\MyPic.jpg");
DataObject obj = new DataObject();
obj.SetFileDropList(s);
myListView.DoDragDrop(obj, DragDropEffects.Copy);
}
2)就是这样!
2) That's it!
这篇关于怎么做拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!