The ListIndex Property of the ListBox
A second way is detect the item selected in the ListBox is to use the ListIndex property of the ListBox. If the word fragment ‘Index’ triggers the recollection of an array, you’re right---as it turns out, as each item is added to a ListBox, a corresponding ‘behind the scenes’ array is created, with an element added for each item in the ListBox. A ListBox containing 13 entries creates a 13 element array, with elements numbered from 0 to 12. The ListIndex property of the ListBox corresponds to the 0 based element number of the selected item in this array---therefore, if ‘Georgia’ is selected in the ListBox, since it’s the third item in the ListBox, the ListIndex property of the ListBox is 2 (remember, the element numbers start with 0). With Georgia selected in the ListBox, let’s go back to the Immediate Window, and type this statement into it
? List1.ListIndex
As you can see, VB is telling us that the item selected in the ListBox is element number 2 (the third item) corresponding to ‘Georgia’
Using these two properties of the ListBox, we can now place code into our top most command button to ‘move’ the selected item from the ListBox on the left to the ListBox on the right. As I mentioned previously, we’ll execute the AddItem Method of the second ListBox, using the Text property of the first ListBox as an argument. Then we’ll execute the RemoveItem method of the first ListBox, using the ListIndex property of the ListBox as an argument (the RemoveItem method requires the numeric Index of the item in the ListBox). Here’s the code…
Private Sub cmdMovetoRight_Click()
If List1.ListIndex = -1 Then Exit Sub
List2.AddItem List1.Text
List1.RemoveItem List1.ListIndex
End Sub
The first thing we do is check to see if the ListIndex property of the ListBox is -1. A Minus One (-1) is Visual Basic’s way to telling us that no item is selected in the ListBox, and it that’s the case, we exit the procedure by executing the Exit Sub statement.
If an item is selected, then we add that item to the right-hand ListBox, and then remove it from the left-hand ListBox using the RemoveItem method. Notice that we add the item to the right-hand ListBox BEFORE removing it from the left-hand ListBox—otherwise our program will bomb.
Now when we execute the program, select ‘Georgia’ in the left-hand ListBox, and then click on the ‘move to right’ command button, the selected item in the left-hand ListBox is moved to the right ListBox.
By the way, to maintain the alphabetical order of the entries in the ListBox when you start moving items around like this, be sure to set the Sorted Property of each ListBox to True.
We can write similar code to ‘move’ items from the right-hand ListBox to the left, like this..
Private Sub cmdMoveToLeft_Click()
If List2.ListIndex = -1 Then Exit Sub
List1.AddItem List2.Text
List2.RemoveItem List2.ListIndex
End Sub
If we now execute the program, we’ll be able to ‘move’ items from one ListBox to another easily.
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:
Komentar (Atom)