• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting/Deleting Multiple Items From Listbox
#1
Description : We are goin' to create 2 Listboxes, add some items in List1 at design time and then with cmdMove command button we'll copy selected items to list2.

Concept : This code checks the whole list(List1) through a for loop and then add the selected items to the other List2

NOTE : Make sure that the multiselect property of listboxes is '1-Simple'

Code:
Private Sub CmdcopyToList2_Click()
Dim i As Integer
If List1.SelCount = 1 Then
List2.AddItem List1.Text
Else
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
List2.AddItem List1.List(i)
End If
Next i
End If
End Sub

Explanation : We are cheking selcount just for learning purposes(i.e, to learn the basic adding procedure).

If more then ONE items are selected, the loops checks each list item one by one and then adds the item if IT IS SELECTED.

NOTE : Step -1 is only for learning purpose, loop can be runned either way.

Code:
Private Sub CmdRemove_Click()
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
List1.RemoveItem (i)
End If
Next i
End Sub

Explanation : Just as the previous one, this too Removes the selected Items.

I Hope this helped.
  Reply
#2
I remember this demo from when I was learning VB.
[Image: nomnomnom.jpg]
;7$=v?%v%#5>v7v8994
The decrypt code is V, I could not make it any simpler!
  Reply


Forum Jump: