C# 鼠标拖动问题

.Net技术 码拜 9年前 (2015-05-11) 1015次浏览 0个评论

在TextBox上实现了文件的拖拽和路径的获取源代码如下:

 private void Form1_DragEnter(object sender, DragEventArgs e)
 {
              if (e.Data.GetDataPresent(DataFormats.FileDrop))
                  e.Effect = DragDropEffects.Link; //重要代码:表明是链接类型的数据,比如文件路径
               else e.Effect = DragDropEffects.None;
    }

 private void Form1_DragDrop(object sender, DragEventArgs e)
 {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
             MessageBox.Show(path);  
 }

但我想问的是能不能实现同时拖动多个文件到TextBox框中,并获取每个文件的路径,我记得有的应用程序是能这样做的,但不知使用C# winform怎么实现???

10分
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
本来就是拖进来了多个,但你却只处理了一个

var a = ((System.Array)e.Data.GetData(DataFormats.FileDrop));
for(int i=0; i<a.Length; i++) {
  MessageBox.Show(a.GetValue(i).ToString());
}
10分
if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                    for (int i = 0; i < files.Length; i++)
                    {
                        
                    }
引用 1 楼 xuzuning 的回复:

string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
本来就是拖进来了多个,但你却只处理了一个

var a = ((System.Array)e.Data.GetData(DataFormats.FileDrop));
for(int i=0; i<a.Length; i++) {
  MessageBox.Show(a.GetValue(i).ToString());
}
引用 2 楼 u012804018 的回复:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                    for (int i = 0; i < files.Length; i++)
                    {
                        
                    }

多谢回答,的确是这样,开始写错了,加了一个GetValue(0),使用循环时一直提示string不能转换为string[],看来我应该多尝试一下。。。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C# 鼠标拖动问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!