Listbox - How do I move selected item up the list 1 position?

  • Thread starter Thread starter Joe Fallon
  • Start date Start date
J

Joe Fallon

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?
 
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
 
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?

' up
Dim item As Object = list.SelectedItem
If Not Item Is Nothing Then
Dim index As Integer = list.Items.IndexOf(item)
If index != 0 Then
list.Items.RemoveAt(index)
index -= 1
list.Items.Insert(index, item)
list.SelectedIndex = index
End If
End If

'down
Dim item As Object = list.SelectedItem
If Not Item Is Nothing Then
Dim index As Integer = list.Items.IndexOf(item)
If index < list.Items.Count - 1 Then
list.Items.RemoveAt(index)
index += 1
list.Items.Insert(index, item)
list.SelectedIndex = index
End If
End If
 
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?
 
I'm glad it worked for you, Joe!
I forgot I was in a VB group :-)

- Pete

Joe Fallon said:
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?
 
Tom,
Thanks for the post.
I did not test it but it looks similar to the other code.

Glad you posted - because if there had been no other code I would have been
all over yours! <g>
 
Back
Top