您的位置:首页 > 编程语言 > C#

c#窗体支持多个文件拖放的代码实现

2010-08-02 13:21 330 查看
//在Form的属性中要设置 AlowDrop为true
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetData(DataFormats.FileDrop) != null)
{
e.Effect = DragDropEffects.Copy;
}

//下面这段代码不支持间隔选中的文件
//if (e.Data.GetDataPresent(DataFormats.FileDrop))
// e.Effect = DragDropEffects.Link;
//else e.Effect = DragDropEffects.None;
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
String[] fileNames = (String[])e.Data.GetData(DataFormats.FileDrop);
int j = listBox1.Items.Count + 1;
for (int i = 0; i < fileNames.Length; i++)
{
listBox1.Items.Add(String.Format("{0:D3} ", i + j) + fileNames[i]);
}

//下面这段代码不支持间隔选中的文件
//string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
//listBox1.Items.Add(path);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: