473,404 Members | 2,137 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,404 software developers and data experts.

Listbox sort problem after upgrading to dotnet

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
Nov 20 '05 #1
3 2173
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

BVH wrote:
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

Nov 20 '05 #2


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3
Your'e welcome

OHM

Bart Van Hecke wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Luna | last post by:
Hi, I want to pass the values that have been chosen from a few comboboxes into a listbox as a single entry. how do i do this?? this is what i tried... it will probably give u some...
4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
4
by: Hardy Wang | last post by:
Hi I have a ListBox control in my ASP.NET page. After I binding data to this control, I would like to be able to change this display order of items in this control. Just like change layout...
0
by: Jason Freeman | last post by:
Hi, (Cross-posted to microsoft.public.dotnet.framework.adonet) I've got a form with a listbox and a textbox on it. Both are bound to a DataView, which is sorted on the "Name" field of the...
11
by: Zorpiedoman | last post by:
The problem is this: I have a list box. I set an array list as the datasource. I remove an item from the array list. I set the listbox datasource to nothing. I set the listbox datasource to...
3
by: Ali Chambers | last post by:
Hi, I have a bit of a problem with a sort procedure I need to do. I have a list of items in a listbox, eg:- 2.3%<A other text here> -4%<B other text here> 10%<C other text here> -9.3%<D...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
by: RamiH | last post by:
Hi, I am using a listbox to show data from a certain table, i want the user, after the listbox is filled, to be able to sort the data according to the column of his choice. I could sort the table...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.