如何实现LISTVIEW中把文件拖拽进文件夹的效果

hsylolicon 2011-08-08 02:48:08
小弟最近在做一个项目,在LISTVIEW中显示电脑某个文件夹中的全部文件和文件夹,如何实现在LISTVIEW中选中一个文件,把它拖拽至该LISTVIEW中一个文件夹的效果,就像WINDOWS那样
先谢谢各位高手了
...全文
567 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
沐NeMo 2011-08-09
  • 打赏
  • 举报
回复
沐NeMo 2011-08-09
  • 打赏
  • 举报
回复
Me.Label1.Text = String.Format("你拖动并释放在 {0} item上", OnItem.Text)
一句改為:
Me.Label1.Text = String.Format("你拖动 {0} 并释放在 {1} 上", item.Text, OnItem.Text)
沐NeMo 2011-08-09
  • 打赏
  • 举报
回复
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ListView1.AllowDrop = True
Me.ListView1.LargeImageList = Me.ImageList1
Dim item As ListViewItem
item = New ListViewItem
item.Name = "1"
item.Text = "first"
item.ImageIndex = 0
item.Tag = 1
Me.ListView1.Items.Add(item)
item = New ListViewItem
item.Name = "2"
item.Text = "second"
item.ImageIndex = 0
item.Tag = 2
Me.ListView1.Items.Add(item)
End Sub

Private Sub ListView1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.Move
End If
End Sub

Private Sub ListView1_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
If Me.ListView1.SelectedItems.Count > 0 Then
Me.ListView1.DoDragDrop(CType(Me.ListView1.SelectedItems.Item(0), ListViewItem), DragDropEffects.All)
End If
End Sub

Private Sub ListView1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
'Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
'Me.Label1.Text = "ok"
'If hit.Item IsNot Nothing Then
' Me.Label1.Text = "你釋放與item:" & hit.Item.Text
'End If
Dim item As ListViewItem
Dim OnItem As ListViewItem
Dim lv As ListView = CType(sender, ListView)
Dim clX As Integer = lv.PointToClient(New Point(e.X, e.Y)).X
Dim clY As Integer = lv.PointToClient(New Point(e.X, e.Y)).Y
If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", False) Then
'dragging a listview item
item = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
OnItem = CType(sender, ListView).GetItemAt(clX, clY)
'destLv.Items.Insert(OnItem.Index, item.Clone)
'item.Remove()
Me.Label1.Text = String.Format("你拖动并释放在 {0} item上", OnItem.Text)
End If
End If
End Sub
End Class
hsylolicon 2011-08-09
  • 打赏
  • 举报
回复
嗯,是的,并且获取到被拖拽的ITEM和拖拽到的ITEM的值
沐NeMo 2011-08-09
  • 打赏
  • 举报
回复
你的意思是:LISTVIEW拖动自己的item放到自己另外一个item里面吗?
hsylolicon 2011-08-09
  • 打赏
  • 举报
回复
别沉啊喂,这个东西弄得我很头大啊……
hsylolicon 2011-08-09
  • 打赏
  • 举报
回复
linjimu 非常感谢,准备结帖了
沐NeMo 2011-08-09
  • 打赏
  • 举报
回复
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{

private void Form1_Load(System.Object sender, System.EventArgs e)
{
this.ListView1.AllowDrop = true;
this.ListView1.LargeImageList = this.ImageList1;
ListViewItem item = default(ListViewItem);
item = new ListViewItem();
item.Name = "1";
item.Text = "first";
item.ImageIndex = 0;
item.Tag = 1;
this.ListView1.Items.Add(item);
item = new ListViewItem();
item.Name = "2";
item.Text = "second";
item.ImageIndex = 0;
item.Tag = 2;
this.ListView1.Items.Add(item);
}

private void ListView1_DragEnter(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
e.Effect = DragDropEffects.Move;
}
}

private void ListView1_ItemDrag(System.Object sender, System.Windows.Forms.ItemDragEventArgs e)
{
if (this.ListView1.SelectedItems.Count > 0) {
this.ListView1.DoDragDrop((ListViewItem)this.ListView1.SelectedItems.Item(0), DragDropEffects.All);
}
}

private void ListView1_DragDrop(System.Object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
//Dim hit As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
//Me.Label1.Text = "ok"
//If hit.Item IsNot Nothing Then
// Me.Label1.Text = "你釋放與item:" & hit.Item.Text
//End If
ListViewItem item = default(ListViewItem);
ListViewItem OnItem = default(ListViewItem);
ListView lv = (ListView)sender;
int clX = lv.PointToClient(new Point(e.X, e.Y)).X;
int clY = lv.PointToClient(new Point(e.X, e.Y)).Y;
if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem", false)) {
//dragging a listview item
item = (ListViewItem)e.Data.GetData("System.Windows.Forms.ListViewItem");
OnItem = ((ListView)sender).GetItemAt(clX, clY);
//destLv.Items.Insert(OnItem.Index, item.Clone)
//item.Remove()
this.Label1.Text = string.Format("你拖动 {0} 并释放在 {1} 上", item.Text, OnItem.Text);
}
}
}
public Form1()
{
Load += Form1_Load;
}
}
hsylolicon 2011-08-09
  • 打赏
  • 举报
回复
那个,有没有C#代码,VB实在是看不懂……(我没发错区啊……
Qiaorui 2011-08-08
  • 打赏
  • 举报
回复
看错题了,这个没做过。
hsylolicon 2011-08-08
  • 打赏
  • 举报
回复
比如把任意一个或多个文件拖拽进入777777里面
hsylolicon 2011-08-08
  • 打赏
  • 举报
回复


从WINDOWS中拖拽到LISTVIEW里的代码倒是还好,就是不明白LISTVIEW当中如何拖拽
Qiaorui 2011-08-08
  • 打赏
  • 举报
回复
漏了重要一项
AllowDrop = true;
Qiaorui 2011-08-08
  • 打赏
  • 举报
回复

MultiSelect = true;
FullRowSelect = true;

private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
// 放
private void listView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);

foreach (String s in files)
{
fi = new FileInfo(s);
ListViewItem li = new ListViewItem();
li.SubItems.Clear();
li.SubItems[0].Text = s.ToString();
(sender as ListView).Items.Add(li);
}
}
}

//拖,我的程序只从文件夹拖到ListView,没用到以下代码,故没有测试,自己改改吧
private void ListView1_ItemDrag(object sender, ItemDragEventArgs e)
{
//判断是否是鼠标右键按动
if (e.Button == MouseButtons.Right)return;

ListViewItem[] myItems = new ListViewItem[((ListView)(sender)).SelectedItems.Count];
int i = 0;
// 循环处理拖放来源的 SelectedItems 集合。
foreach(ListViewItem myItem in ((ListView)(sender)).SelectedItems)
{
// 将ListViewItem新增至ListViewItems的数组中。
myItems[i] = myItem;
i = i + 1;
}
// 建立一个DataObject对象来包含ListViewItem的数组。
((ListView)(sender)).DoDragDrop(new DataObject("System.Windows.Forms.ListViewItem()", myItems), DragDropEffects.Move);
}
jy251 2011-08-08
  • 打赏
  • 举报
回复
listview不是有drag的事件么
hsylolicon 2011-08-08
  • 打赏
  • 举报
回复
如果可以最好能把多文件拖拽的代码发下,非常感谢

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧