C# 将文件拖放到文本框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9464001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
drag and drop file into textbox
提问by michelle
I want to drag and drop a file so that the textbox shows the full file path. I have used the drag enter and drag drop events but I find that they are not entering the events.
我想拖放一个文件,以便文本框显示完整的文件路径。我已经使用了拖动输入和拖放事件,但我发现它们没有进入事件。
private void sslCertField_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
e.Effect = DragDropEffects.All;
}
}
private void sslCertField_DragEnter(object sender, DragEventArgs e)
{
string file = (string)e.Data.GetData(DataFormats.FileDrop);
serverURLField.Text = file;
}
Can anyone point out what I am doing wrong?
谁能指出我做错了什么?
UPDATE: Does not work if program is set to run with elevated permissions (vista/win 7)
更新:如果程序设置为以提升的权限运行(vista/win 7),则不起作用
采纳答案by max
Check the AllowDropproperty of your textbox - it should be set to true.
Also, convert drag-drop data to string[]in case of DataFormats.FileDrop, not just string:
检查AllowDrop文本框的属性 - 它应该设置为true. 此外,将拖放数据转换string[]为DataFormats.FileDrop,而不仅仅是string:
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if(files != null && files.Length != 0)
{
serverURLField.Text = files[0];
}
And I think you should swap code in your drag event handlers - usually you show user that drag-drop is possible in DragEnterand perform actual operation on DragDrop.
而且我认为您应该在拖动事件处理程序中交换代码 - 通常您向用户展示拖放是可能的,DragEnter并在DragDrop.
回答by jayesh
dont run it from visual studio... run the .exe which is created once you build your solution.. hope that helps :)
不要从visual studio运行它...运行构建解决方案后创建的.exe ..希望有帮助:)
回答by Joao Coelho
Elevated privileges should not have anything to do with it. You also need to implement the DragOverevent in addition to the DragDropthat Max answered. This is the code that should be added for DragDrop:
提升的特权不应该与它有任何关系。DragOver除了DragDropMax 回答的事件之外,您还需要实现该事件。这是应该为 DragDrop 添加的代码:
private void textBoxFile_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void textBoxFile_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
回答by Mickael V.
If you're using WPF and it still doesn't work with the answers here (which was my case), you need to use
如果您使用的是 WPF 并且它仍然无法处理这里的答案(这是我的情况),则需要使用
e.Handled = true;
in the PreviewDragEnterevent, as described hereand here(they're the same article, but just in case one goes down).
在这种情况PreviewDragEnter下,如此处和此处所述(它们是同一篇文章,但以防万一)。
Here is the code snippet, from that source :
这是来自该来源的代码片段:
private void TextBox_PreviewDragEnter(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
private void TextBox_PreviewDrop(object sender, DragEventArgs e)
{
object text = e.Data.GetData(DataFormats.FileDrop);
TextBox tb = sender as TextBox;
if (tb != null)
{
tb.Text = string.Format("{0}", ((string[])text)[0]);
}
}
回答by Martijn
If your visual studio is running under Administrator rights Drag and drop functionality seems not to work.
如果您的 Visual Studio 在管理员权限下运行,则拖放功能似乎不起作用。
=> Run visual studio without Admin rights and it will work
=> 在没有管理员权限的情况下运行visual studio,它会工作

