温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET如何实现表间拖放

发布时间:2021-12-01 17:12:29 来源:亿速云 阅读:224 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“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如何实现表间拖放”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI