ListView SelectedItem屬性不會返回在您的ListView上選擇的項目集合,因此您不能顯式獲取第一個選定項目,第二個選定項目等。您將需要循環遍歷所有ListItems ListView並檢查是否每個都被選中。如果是,就做你想做的事。
我在示例代碼中看到的一個問題是您使用的是ListView DblClick事件。我可能是錯的,但它看起來每當它觸發時,只能選擇一個ListView項目(觸發該事件的項目)。解決這個問題的方法是將你的代碼放到一個新的程序中。這是應該工作的一個:
Private Sub GetSelectedItems()
' Make sure exactly two items are selected on our ListView.
If (CheckListViewSelectedItemCount(listview1, 2)) Then
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
' Find out which items are selected.
For i = 1 To listview1.ListItems.Count
If (listview1.ListItems(i).Selected) Then
' Assign the Text of the 'first' selected item to Textbox1.Text.
If (Not blnFoundFirstItem) Then
Textbox1.Text = listview1.ListItems(i).Text
blnFoundFirstItem = True
' Assign the Text of the 'second' selected item to Textbox2.Text.
Else
Textbox2.Text = listview1.ListItems(i).Text
End If
End If
Next i
Else
MsgBox "You need to select two items."
End If
End Sub
我不知道在我的For循環中迭代通過哪種順序ListItems。在我的代碼中,可能要分配給Textbox1.Text的內容可能要分配給Textbox2.Text。
您需要的代碼至少在ListView上選擇了兩個項目。我不知道VB6是否有辦法返回所選項目的數量,所以我寫了一個小功能來做到這一點:
' Return True if the passed ListView control has a number of selected items that's equal to the intExpectedItemCount parameter.
Private Function CheckListViewSelectedItemCount(listView As listView, intExpectedItemCount As Integer) As Boolean
Dim intSelectedItemCount As Integer
intSelectedItemCount = 0
Dim i As Integer
For i = 1 To listView.ListItems.Count
If (listView.ListItems(i).Selected) Then
intSelectedItemCount = intSelectedItemCount + 1
End If
Next i
CheckListViewSelectedItemCount = (intSelectedItemCount = intExpectedItemCount)
End Function