The ListIndex Property of the ListBox 3
Never fear! We can make this work, we just need to modify the code to use that ‘behind the scenes’ array I keep talking about. What we’ll do is use Loop processing to take advantage of a property of the ListBox I haven’t mentioned yet, the Selected Property. I’ll display the code first, and then I’ll explain it…
Private Sub cmdMovetoRight_Click()
Dim i As Integer
If List1.ListIndex = -1 Then Exit Sub
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
List2.AddItem List1.List(i)
List1.RemoveItem i
End If
Next i
End Sub
First of all, those of you who have read my books know that I never name variables with a single character, but I’m trying to make sure that the code sits into the width of a code window (the variable ‘I’ really should be ‘intCounter’).
With that out of the way, what we’re doing here is using a loop (in this case a For…Next Loop) to move through the elements of that ‘behind the scenes’ array I’ve been mentioning. The first element in the array is 0, and the last element is equal to the total number of elements in the array, minus 1 (that’s because the array is numbered starting with the number 0). In other words, if the ListBox contains 5 items, it’s last element is number 4.
How can we determine the total number of items in the ListBox (or the number of elements in the array?)—through the ListCount Property of the ListBox. In effect, this line of code…
For i = List1.ListCount - 1 To 0 Step -1
tells Visual Basic to execute a For…Next loop starting with the last element of the array and work our way backwards to 0—the first element of the array representing the ListBox. Working backwards through the array is necessary because if we started from the 0 element of the array, Visual Basic would re-sequence the elements in the Array, and eventually we would attempt to remove an item number that no longer exists).
What we need now is a way to determine which element of the array is selected---and that’s where the Selected Property of the ListBox comes into play.
source: http://www.vbexplorer.com/VBExplorer/VBExplorer.asp
------------------------------------------
Related:
single-record
Trik Cara Melacak IP
Trik Choose Phone
Trik woodwork-stores
Trik woodwork supplies
Trik wood work tool
Using radio button vb2008
Valentineday
vb2008 with control
vb2008 with control properties
vb2008 with oriented programming
VB.NET Solution files
VB.NET Code myclass
Senin, 09 Februari 2009
Langganan:
Postingan (Atom)