简单讲述VB.NET表间拖放

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

经过长时间学习VB.NET表间拖放,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西。

VB.NET表间拖放

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

  1. Private Sub ListView_ItemDrag(ByVal sender As Object, ByVal e As _  
  2. System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag, _  
  3. ListView2.ItemDrag  
  4. Dim myItem As ListViewItem  
  5. Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem  
  6. Dim i As Integer = 0 
  7.  
  8. ' Loop though the SelectedItems collection for the source.  
  9. For Each myItem In sender.SelectedItems  
  10. ' Add the ListViewItem to the array of ListViewItems.  
  11. myItems(i) = myItem  
  12. ii = i + 1  
  13. Next  
  14. ' Create a DataObject containg the array of ListViewItems.  
  15. sender.DoDragDrop(New _  
  16. DataObject(System.Windows.Forms.ListViewItem(), myItems), _  
  17. DragDropEffects.Move)  
  18. End Sub  
  19.  
  20. Private Sub ListView_DragEnter(ByVal sender As Object, ByVal e As _  
  21. System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, _  
  22. ListView2.DragEnter  
  23. ' Check for the custom DataFormat ListViewItem array.  
  24. If e.Data.GetDataPresent(System.Windows.Forms.ListViewItem()) Then  
  25. e.Effect = DragDropEffects.Move  
  26. Else  
  27. e.Effect = DragDropEffects.None  
  28. End If  
  29. End Sub  
  30.  
  31. Private Sub ListView_DragDrop(ByVal sender As Object, ByVal e As _  
  32. System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, _  
  33. ListView2.DragDrop  
  34. Dim myItem As ListViewItem  
  35. Dim myItems() As ListViewItem = _ e.Data.GetData(System.Windows.Forms.ListViewItem())  
  36. Dim i As Integer = 0 
  37.  
  38. For Each myItem In myItems  
  39. ' Add the item to the target list.  
  40. sender.Items.Add(myItems(i).Text)  
  41. ' Remove the item from the source list.  
  42. If sender Is ListView1 Then  
  43. ListView2.Items.Remove(ListView2.SelectedItems.Item(0))  
  44. Else  
  45. ListView1.Items.Remove(ListView1.SelectedItems.Item(0))  
  46. End If  
  47. ii = i + 1  
  48. Next  
  49. End Sub 


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

VB.NET表间拖放结论:

正像你从这些例子中所看到的一样,为应用程序添加拖放操作并不是很难。当你理解了这些基本的技巧后,你就可以为你自己的程序添加拖放的代码了 。

【编辑推荐】

  1. 分析VB QuickSort应用程序
  2. 如何掌握强大的VB.NET ReadLine()方法
  3. 讲述强大的VB.NET Web Forms,使用起来却如此简单
  4. 两步就可以掌握VB使用ArrayList类
  5. VB.NET应用程序的入门指南
责任编辑:佚名 来源: IT168
相关推荐

2009-10-16 13:26:53

VB.NET Exce

2009-10-19 08:55:22

VB.NET多重继承

2009-10-15 11:11:08

VB.NET Text

2009-10-15 16:39:00

VB.NET读取INI

2009-10-14 17:08:44

VB.NET使用Fil

2009-10-26 09:50:11

C#与VB.NET

2009-10-21 10:45:50

VB.NET Quic

2009-11-02 15:45:03

VB.NET IEnu

2009-10-13 17:03:55

VB.NET面向对象

2009-10-12 16:39:59

OracleTransVB.NET使用

2009-10-16 09:35:24

VB.NET制作透明窗

2009-10-23 13:22:25

VB.NET实现拖动图

2009-10-14 11:15:06

VB.NET Grou

2009-10-10 16:44:52

VB.NET开发控件

2009-10-23 14:58:05

VB.NET传输表空间

2010-01-13 17:47:59

VB.NET拖放

2009-10-27 11:39:03

VB.NET事件处理程

2009-10-26 18:11:47

VB.NET调用Exc

2009-10-29 09:57:16

VB.NET实现数据绑

2009-10-22 09:20:46

VB.NET Proc
点赞
收藏

51CTO技术栈公众号