如何将listView中的项拖到另一个listview

liao5930 2008-10-24 03:33:38
如题,最好能够出现拖动的效果
...全文
196 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcyzq 2008-10-24
  • 打赏
  • 举报
回复
顶。
mykelly6 2008-10-24
  • 打赏
  • 举报
回复
嗯,用Drag and Drop做就好了~
chinawes 2008-10-24
  • 打赏
  • 举报
回复
学习了
smallfile 2008-10-24
  • 打赏
  • 举报
回复
楼上正解




mark
cpio 2008-10-24
  • 打赏
  • 举报
回复
下面的代码示例演示在两个 ListBox 控件之间的拖放操作。当拖动动作启动时,该示例调用 DoDragDrop 方法。在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation.DragSize,则启动拖动动作。IndexFromPoint 方法用于在 MouseDown 事件期间确定要拖动的项的索引。

该示例同时演示了对拖放操作使用自定义光标。示例假定应用程序目录中存在两个光标文件:3dwarro.cur 和 3dwno.cur,分别用于自定义拖动光标和禁止停放光标。如果选中 UseCustomCursorsCheckCheckBox,则使用自定义光标。自定义光标在 GiveFeedback 事件处理程序中设置。

键盘状态在右 ListBox 的 DragOver 事件处理程序中计算,以根据 Shift、Ctrl、Alt 或 Ctrl+Alt 键的状态确定将发生哪种拖动操作。放置动作在 ListBox 中发生的位置也在 DragOver 事件期间确定。如果要放置的数据不是一个 String,则 DragEventArgs.Effect 将被设置为 DragDropEffects.None。最后,停放状态在 DropLocationLabelLabel 中显示。

要放置的用于右 ListBox 的数据在 DragDrop 事件处理程序中确定,并且在 ListBox 中的适当位置添加该 String 值。如果拖动操作移动到窗体边框的外面,则 QueryContinueDrag 事件处理程序中将取消拖放操作。

此代码片段演示如何协同使用 QueryContinueDragEventArgs 类和 QueryContinueDrag 事件。有关完整的代码实例,请参见 DoDragDrop 方法。

Visual Basic 复制代码
Private Sub ListDragSource_QueryContinueDrag(ByVal sender As Object, ByVal e As QueryContinueDragEventArgs) Handles ListDragSource.QueryContinueDrag
' Cancel the drag if the mouse moves off the form.
Dim lb as ListBox = CType(sender, System.Windows.Forms.ListBox)

If Not (lb is nothing) Then

Dim f as Form = lb.FindForm()

' Cancel the drag if the mouse moves off the form. The screenOffset
' takes into account any desktop bands that may be at the top or left
' side of the screen.
If (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) Or _
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) Or _
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) Or _
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) Then

e.Action = DragAction.Cancel
End If
End if
End Sub


C# 复制代码
private void ListDragSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e) {
// Cancel the drag if the mouse moves off the form.
ListBox lb = sender as ListBox;

if (lb != null) {

Form f = lb.FindForm();

// Cancel the drag if the mouse moves off the form. The screenOffset
// takes into account any desktop bands that may be at the top or left
// side of the screen.
if (((Control.MousePosition.X - screenOffset.X) < f.DesktopBounds.Left) ||
((Control.MousePosition.X - screenOffset.X) > f.DesktopBounds.Right) ||
((Control.MousePosition.Y - screenOffset.Y) < f.DesktopBounds.Top) ||
((Control.MousePosition.Y - screenOffset.Y) > f.DesktopBounds.Bottom)) {

e.Action = DragAction.Cancel;
}
}
}


cpio 2008-10-24
  • 打赏
  • 举报
回复

首先,把控件的AllowDrop属性设置为true

然后,在鼠标按下事件里面调用它的DoDragDrop方法,最好在鼠标按下并开始移动的时候调用。

在DragDrop事件里面表示播放完成

110,537

社区成员

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

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

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