Listbox sort problem after upgrading to dotnet

B

BVH

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 buttons act
as ‘up' and ‘down' buttons. The listbox contains some items that need
to be sorted "BY THE USER" (That's why I needed the 2 command buttons)

A little example :

Suppose the listbox contains the Items :
1
2
3
4
5

If a user selects the Item on position 3 and clicks the "Up"-button,
the item on position 3 is moved to position 2 and the item on position
2 is moved to position 3.
Exact the opposite is done when the user clicks the "down" button.

I hope I make myself clear and that my English is understandable.

Now, here comes the actual problem : this code worked fine for me in
VB6, but I'm unable to run the code properly with VB.NET.
I tried the upgrade wizard and rewrote the code, but apparently I must
be doing something wrong.

The VB6 code is posted below. If anyone can help me out, I'd really
appreciate it, cause I need this thing up and running very soon.

Thankz and Greetz,

BVH


Private Sub Up_Click()
On Error Resume Next
Dim lowId As Long, lowText As String
Dim upperId As Long, upperText As String
Dim i As Integer
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
If i = 0 Then Exit For
lowId = List1.ItemData(i)
lowText = List1.List(i)
upperId = i - 1
List1.ListIndex = upperId
upperText = List1.Text
List1.List(upperId) = lowText
List1.ItemData(upperId) = lowId
List1.ItemData(i) = upperId
List1.List(i) = upperText
Exit For
End If

Next i

End Sub
Private Sub Down_Click()
On Error Resume Next
Dim lowId As Long, lowText As String
Dim upperId As Long, upperText As String
Dim i As Integer
For i = 0 To List1.ListCount + 1
If List1.Selected(i) Then
If i = List1.ListCount - 1 Then Exit For
lowId = List1.ItemData(i)
lowText = List1.List(i)
upperId = i + 1
List1.ListIndex = upperId
upperText = List1.Text
List1.List(upperId) = lowText
List1.ItemData(upperId) = lowId
List1.ItemData(i) = upperId
List1.List(i) = upperText
Exit For
End If

Next i

End Sub
 
O

One Handed Man

For the up button, try this and then us the same logic for the down button.

Best Regards - OHM
Dim i, iAbove As Int32

Dim ti, tIAbove As String

i = List1.SelectedIndex

If i > 0 Then

iAbove = i - 1

ti = List1.SelectedItem.ToString()

tIAbove = List1.Items.Item(iAbove).ToString()

List1.Items.Item(i) = tIAbove

List1.Items.Item(iAbove) = ti

End If
 
B

Bart Van Hecke

just tried your suggested solution and it worked fine for me. Just what
I needed....
Thanks for your quick reply ....
I owe you one....
cheers,

BVH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top