双击数据时,将数据从一个列表视图传输到另一个列表视图

时间:2012-12-24 17:37:01

标签: vb.net listview

我有2个ListView设置。 当用户双击任何数据时,Listview1需要将数据传递给listview2。 我该如何存档?我正在使用vb 2008。

这是图像: image

1 个答案:

答案 0 :(得分:1)

这很简单,但它会给你一个起点。请注意,有许多方法可以解决此问题,您需要找出任何验证,例如您的应用程序所需的验证。最大的障碍似乎是抓住对双击目标的项目的引用(重要的是,确保如果用户双击ListView控件的空白区域,则不添加最后选择的项目错误的。

希望这会有所帮助:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ListView1.FullRowSelect = True
        Me.ListView2.FullRowSelect = True
    End Sub


    Private Sub AddItemToSecondList(ByVal item As ListViewItem)
        ' NOTE: We separate this part into its own method so that 
        ' items can be added to the second list by other means 
        ' (such as an "Add to Purchase" button)

        ' ALSO NOTE: Depending on your requirements, you may want to 
        ' add a check in your code here or elsewhere to prevent 
        ' adding an item more than once.
        Me.ListView2.Items.Add(item.Clone())

    End Sub


    Private Sub ListView1_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick

        ' Use the HitTest method to grab a reference to the item which was
        ' double-clicked. Note that if the user double-clicks in an empty
        ' area of the list, the HitTestInfo.Item will be Nothing (which is what 
        ' what we would want to happen):
        Dim info As ListViewHitTestInfo = Me.ListView1.HitTest(e.X, e.Y)

        'Get a reference to the item:
        Dim item As ListViewItem = info.Item

        ' Make sure an item was the trget of the double-click:
        If Not item Is Nothing Then
            Me.AddItemToSecondList(item)
        End If

    End Sub


End Class