0

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

这是图像: 图片

4

1 回答 1

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
于 2012-12-25T15:43:52.687 回答