新西兰服务器

VB.NET如何实现表间拖放


VB.NET如何实现表间拖放

发布时间:2021-12-01 17:12:29 来源:高防服务器网 阅读:58 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“VB.NET如何实现表间拖放”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“VB.NET如何实现表间拖放”这篇文章吧。

VB.NET表间拖放

VB.NET表间拖放有一个情况是从一个列表移动项目到另一个列表。这种情况下拖放将变得更加简单。向窗体中添加两个ListView控件,并把他们的AllowDrop、Multiselect、View属性分别设置成True、True、List。并添加如下代码:

Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _  System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag, _  ListView2.ItemDrag  Dim myItem As ListViewItem  Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem  Dim i As Integer = 0  ' Loop though the SelectedItems collection for the source.  For Each myItem In sender.SelectedItems  ' Add the ListViewItem to the array of ListViewItems.  myItems(i) = myItem  ii = i + 1  Next  ' Create a DataObject containg the array of ListViewItems.  sender.DoDragDrop(New _  DataObject(System.Windows.Forms.ListViewItem(), myItems), _  DragDropEffects.Move)  End Sub   Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _  System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, _  ListView2.DragEnter  ' Check for the custom DataFormat ListViewItem array.  If e.Data.GetDataPresent(System.Windows.Forms.ListViewItem()) Then  e.Effect = DragDropEffects.Move  Else  e.Effect = DragDropEffects.None  End If  End Sub   Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As _  System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, _  ListView2.DragDrop  Dim myItem As ListViewItem  Dim myItems() As ListViewItem = _ e.Data.GetData(System.Windows.Forms.ListViewItem())  Dim i As Integer = 0  For Each myItem In myItems  ' Add the item to the target list.  sender.Items.Add(myItems(i).Text)  ' Remove the item from the source list.  If sender Is ListView1 Then  ListView2.Items.Remove(ListView2.SelectedItems.Item(0))  Else  ListView1.Items.Remove(ListView1.SelectedItems.Item(0))  End If  ii = i + 1  Next  End Sub

你可能不明白为什么这个例子中用的是ListView控件而不是ListBox控件,这个问题题的好,因为ListBox控件不支持多项拖放。ListView和TreeView控件有个ItemDrag事件。上面的例子中,一个ItemDrag事件句柄覆盖了两个控件,并在列在Handles从句。Sender参数表明哪个控件正在初始化Drag。因为DataFormats类没有ListViewItem类型成员,数据必须传递给一个系统类型。ItemDrag创建了一个ListViewItem类型的数组,并用一个循环来遍历SelectedItem集合。在DoDragDrop方法中,创建了一个新的DataObject并用数组来来对它进行操作。可以用这种方法来拖放任何系统类型。

以上是“VB.NET如何实现表间拖放”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注高防服务器网行业资讯频道!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[