[载转] C#中让ListBox支持文件路径的拖放
作者: 佚名 来源: java060515 日期: 09-04-13
提交者:潘巨华
查看: 1529
有时,我们程序,需要 载一个文件列表,这个列表,通常用ListBox来存放,为了方便操作,让listBox支持拖放入文件的路径,是个非常好的功能,在.NET里面实现这个功能,是非常方便,只需要3个步骤:
将 ListBox 的 AllowDrop 属性设为 true
在 ListBox 的 DragOver 和 DragEnter 事件中处理
void ListBox1DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
void ListBox1DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
上面的 e.Effect 可以 据实际情况,调节不同的值,不过,如果不确定,就用默认的All
3. 处理拖放事件
void ListBox1DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach (String s in files)
{
(sender as ListBox).Items.Add(s);
}
}
评论信息
发表评论
单击这里登录 Cookcode.net 提交文章评伦


