C# 文件拖放在listbox上不起作用
这是我第一次使用拖放。因此,我有一个带有C# 文件拖放在listbox上不起作用,c#,winforms,visual-studio,drag-and-drop,listbox,C#,Winforms,Visual Studio,Drag And Drop,Listbox,这是我第一次使用拖放。因此,我有一个带有列表框的表单,没有其他内容。我希望能够将文件从桌面或windows资源管理器拖放到我的列表框中。这是我的密码。少了什么 表格: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listBox1_DragEnter(object
列表框的表单
,没有其他内容。我希望能够将文件从桌面或windows资源管理器拖放到我的列表框中。这是我的密码。少了什么
表格:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
int i;
for (i = 0; i < s.Length; i++)
listBox1.Items.Add(s[i]);
}
}
我做了这个,我想这个会好的,不需要德拉戈弗
private void listBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox_DragDrop(object sender, DragEventArgs e)
{
if (listBox.Items.Count != 0)
{
listBox.Items.Clear();
}
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
int i;
for (i = 0; i < s.Length; i++)
listBox.Items.Add(Path.GetFileName(s[i]));
}
private void listBox\u DragEnter(对象发送方,DragEventArgs e)
{
if(例如Data.GetDataPresent(DataFormats.FileDrop))
e、 效果=DragDropEffects.All;
其他的
e、 效果=DragDropEffects。无;
}
私有无效列表框_DragDrop(对象发送方,DragEventArgs e)
{
如果(listBox.Items.Count!=0)
{
listBox.Items.Clear();
}
string[]s=(string[])e.Data.GetData(DataFormats.FileDrop,false);
int i;
对于(i=0;i
您在DragOver事件中拥有什么?请具体说明您想要的效果,只有DragDropEffects。复制才有意义。并删除DragOver事件处理程序。@HansPassant我希望将文件名添加到我的列表框中。我还删除了DragOver
事件处理程序。那么,调试器告诉您什么?如果您根本没有获得DragEnter事件,那么您需要在没有提升的情况下运行Visual Studio。Windows不允许从非提升的应用程序拖放到提升的应用程序。UIPI禁止它,这是UAC鲜为人知的方面。一切都没有改变。当我把一个文件拖到列表框中时,我仍然会得到一个“不允许”图标。如果我的表单的AllowDrop
选项也设置为true
,AllowDrop为true。我发现一件事,像回收站这样的图标不能做。请检查此项:)我不这么认为,但我不确定。
private void listBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox_DragDrop(object sender, DragEventArgs e)
{
if (listBox.Items.Count != 0)
{
listBox.Items.Clear();
}
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
int i;
for (i = 0; i < s.Length; i++)
listBox.Items.Add(Path.GetFileName(s[i]));
}