By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,222 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

--
Joe Fallon


Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
private void buttonUP_Click(object sender, System.EventArgs e)
{
listBox1.Items.Insert(listBox1.SelectedIndex+1,lis tBox1.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,listB ox1.Items[listBox1.Selecte
dIndex + 1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex+1);
}

- Pete

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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?
--
Joe Fallon

Nov 20 '05 #2

P: n/a
In article <eR**************@TK2MSFTNGP10.phx.gbl>, Joe Fallon wrote:
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

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 23 Hours, 9 Minutes, 42 Seconds
Nov 20 '05 #3

P: n/a
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" <x@x.x> wrote in message
news:o5******************@newsread2.news.pas.earth link.net...
private void buttonUP_Click(object sender, System.EventArgs e)
{
listBox1.Items.Insert(listBox1.SelectedIndex+1,lis tBox1.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,listB ox1.Items[listBox1.Selecte dIndex + 1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex+1);
}

- Pete

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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?

--
Joe Fallon


Nov 20 '05 #4

P: n/a
I'm glad it worked for you, Joe!
I forgot I was in a VB group :-)

- Pete

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <x@x.x> wrote in message
news:o5******************@newsread2.news.pas.earth link.net...
private void buttonUP_Click(object sender, System.EventArgs e)
{

listBox1.Items.Insert(listBox1.SelectedIndex+1,lis tBox1.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,listB ox1.Items[listBox1.Selecte
dIndex + 1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex+1);
}

- Pete

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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?

--
Joe Fallon



Nov 20 '05 #5

P: n/a
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>
--
Joe Fallon

"Tom Shelton" <to*@mtogden.com> wrote in message
news:uY*************@tk2msftngp13.phx.gbl...
In article <eR**************@TK2MSFTNGP10.phx.gbl>, Joe Fallon wrote:
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

--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 1 Build 2600
System Up Time: 0 Days, 23 Hours, 9 Minutes, 42 Seconds

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.