我有2个ListView
设置。
当用户双击任何数据时,Listview1
需要将数据传递给listview2
。
我该如何存档?我正在使用vb 2008。
这是图像:
答案 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