473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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
5 24746
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
4
by: John Guarnieri | last post by:
Hi All, I need some code to drag items in a list box either up or down along with not just the text but with the itemdata too. Can anyone hook me up? TIA John
8
by: Bill | last post by:
I'm trying to create a wizardlike interface using a couple listboxes. I know you've seen it before. You double click on an item in one listbox and it "moves" it to the other. I used to approach...
1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
10
by: yop | last post by:
All When I try to get the text from my listbox I am get an error which is listed below. Any ideas? Thanks Object reference not set to an instance of an object.
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
1
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control...
3
by: BVH | last post by:
Hi, I'm currently having a problem with a vb6 project I once wrote that needs to be converted to vb.NET. The problem is as follows : On a form I have a listbox and two commandbuttons. The 2...
7
by: Lit | last post by:
Hi, How can I capture the vertical scroll bar position for a Listbox. I have a Listbox of 100 items + when I click on it I post back remove the item selected. After returning to the client...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.