472,780 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 24677
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.