Pete,
Thanks a lot! It was exactly what I was looking for.
Once you see the answer it always looks easier...
I had noticed that the indexes changed when items were removed but had not
got around to figuring out how to account for it.
I was hoping I wouldn't have to re-invent the wheel and you came through!
Thanks again.
I translated to VB.Net and add a couple of checks:
There was a minor problem with the MoveDown code which I fixed.
Private mSelectedIndex, mOtherIndex As Integer
============================================================================
==================================
Private Sub btnLogonMoveUp_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogonMoveUp.Click
'do nothing if the top item is selected.
If Me.lst1.SelectedIndex <> 0 Then
mSelectedIndex = Me.lst1.SelectedIndex
mOtherIndex = mSelectedIndex - 1
lst1.Items.Insert(mSelectedIndex + 1, lst1.Items(mOtherIndex))
lst1.Items.RemoveAt(mOtherIndex)
End If
End Sub
============================================================================
==================================
Private Sub btnLogonMoveDown_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnLogonMoveDown.Click
'do nothing if the bottom item is selected.
If Me.lst1.SelectedIndex <> Me.lst1.Items.Count - 1 Then
mSelectedIndex = Me.1.SelectedIndex
mOtherIndex = mSelectedIndex + 1
lst1.Items.Insert(mSelectedIndex, lst1.Items(mOtherIndex))
lst1.Items.RemoveAt(mOtherIndex + 1)
End If
End Sub
--
Joe Fallon
AirPete said:
private void buttonUP_Click(object sender, System.EventArgs e)
{
listBox1.Items.Insert(listBox1.SelectedIndex+1,listBox1.Items[listBox1.Selec
tedIndex-1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex-1);
}
private void buttonDOWN_Click(object sender, System.EventArgs e)
{
listBox1.Items.Insert(listBox1.SelectedIndex,listBox1.Items[listBox1.Selecte
dIndex + 1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex+1);
}
- Pete
Joe Fallon said:
I have a list box with 7 text values in it.
I have a pair of buttons to Move Up or Move Down the selected item one
position.
What is the simplest way to code the buttons so the item moves one position?