need help with list box up and down buttons

jlcvt

New member
Joined
Apr 7, 2007
Messages
4
Programming Experience
Beginner
I have a list box with a list of names in it and I'm trying to add a up button and a down button to the form to move a name in the list up or down. I been able to move the focus to the name on the list up or down with .SelectIndex . But, the order of names stays the same. How do I move a name up or down one spot.
 
For example like this, get the selected index and item, first RemoveAt then Insert:
VB.NET:
Expand Collapse Copy
Dim ix As Integer = ListBox1.SelectedIndex
Dim s As String = ListBox1.Items(ix)
ListBox1.Items.RemoveAt(ix)
ListBox1.Items.Insert(ix - 1, s)
 
Thank you ! That really helped me.

One last question about this topic.
How do I go or check the last last name in the a list? I have a check for the top name on the UP button, and a check when there is no name in the list. Now I'm trying to set up a check for the DOWN button and the last item (name) in the list. I guees I'm trying to find the last SelectedIndex number in the list.

This is what I did for the UP button:

VB.NET:
Expand Collapse Copy
If lstSelPlayers.SelectedIndex = -1 Then
MessageBox.Show("You must first select a player to move.", _
"About your selection...", _
MessageBoxButtons.OK, _
MessageBoxIcon.Warning)
ElseIf lstSelPlayers.SelectedIndex = 0 Then
MessageBox.Show("Play already at the top of the list.", _
"About your selection...", _
MessageBoxButtons.OK, _
MessageBoxIcon.Warning)
Else
Dim intUp As Integer = lstSelPlayers.SelectedIndex
Dim strUp As String = lstSelPlayers.Items(intUp)
lstSelPlayers.Items.RemoveAt(intUp)
lstSelPlayers.Items.Insert(intUp - 1, strUp)
lstSelPlayers.SelectedIndex = (intUp - 1)
End If
For the Down Button:
For the Down button I just changed the intUp to intDown and strUp to strDown
 
Last edited by a moderator:
The other boundary your need is lstSelPlayers.Items.Count. Remember the selectedindex is zero-based.
 
ok I'll use lstSelPlayers.List.Count but, how do I find out what he last number or highest number is? I'm not sure what to put after lstSelPlayers.List.Count = x . To do my check.
 
If you mean 'current' then this is SelectedIndex.
 
Back
Top