在C#.net中拖放编码 [英] Drag Drop Coding in C#.net
本文介绍了在C#.net中拖放编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
plz rply
我想要设计拖拉控件的小型演示
解决方案
用于放弃:
用于拖放:
1)将表单的AllowDrop属性设置为True
2)为表单的DragEnter和DragDrop事件创建一个处理程序。
3)在DragEnter处理程序中,添加以下代码行:
e.Effect = DragDropEffects.Move;
4)在DragDrop处理程序中,添加以下内容:
string [] files =( string [])e.Data.GetData(DataFormats.FileDrop);
if (files!= null )
{
foreach ( string file in files)
{
Console.WriteLine(file); // 或者您需要做的任何事情......
}
}
(显示文件丢弃,还有其他格式可用)
拖动:
句柄MouseMove并创建一个StringCollection,然后创建DataObject,然后执行DragDrop:
private void dgvTracks_MouseMove( object sender,MouseEventArgs e)
{
DataGridView dgv = sender as DataGridView;
System.Collections.Specialized.StringCollection filePath = new System.Collections.Specialized.StringCollection();
foreach (DataGridViewRow row in dgv.SelectedRows)
{
filePath.Add(( string )row.Cells [ 路径跨度>]值)。
}
DataObject dataObject = new DataObject();
dataObject.SetFileDropList(filePath);
dgv.DoDragDrop(dataObject,DragDropEffects.Copy);
}
这提供了一个来自DataGridView的文件列表。
这是一个类似的程序,取决于你想要拖出你的控制(多个)
plz rply
I want to small demo of drag drop control with design
解决方案
For drop:
For drag and drop:
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) 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... } }(this shows file drop, there are other formats available)
For Drag:
Handle MouseMove and create a StringCollection, then the DataObject, then do the DragDrop:
private void dgvTracks_MouseMove(object sender, MouseEventArgs e) { DataGridView dgv = sender as DataGridView; System.Collections.Specialized.StringCollection filePath = new System.Collections.Specialized.StringCollection(); foreach (DataGridViewRow row in dgv.SelectedRows) { filePath.Add((string)row.Cells["Path"].Value); } DataObject dataObject = new DataObject(); dataObject.SetFileDropList(filePath); dgv.DoDragDrop(dataObject, DragDropEffects.Copy); }This provides a list of files from a DataGridView.
It''s a similar procedure depending on what you want to drag out of your control(s)
这篇关于在C#.net中拖放编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文