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

Listbox - How do I sort?

Hello All -

I have 2 list boxes. Items in Left list box populated from a DB. I remove
an item from the left box and add an item to the right box to allow user
selection. When that item is removed again, the item is in the bottom of
the listbox on the left.

Here is the remove code snippet.... How do I sort to original alphabetical
state when the item is removed?

Dim list As New ArrayList

For Each item As ListItem In lstDest.Items

If item.Selected Then

lstSource.Items.Add(item)

list.Add(item)

End If

Next

For Each item As ListItem In list

If item.Selected Then

lstDest.Items.Remove(item)

End If

Next

Thanks to ALL
Nov 18 '05 #1
1 2132
I had the same issue so I wrote this code to make it work:
================================================== ==
This Enum is used to define which kind of comparison you need to make. (I
only built the two that I need so far but once you see how to do it you can
easilt extend it.)

Public Enum ListBoxComparer
[String]
[Decimal]
End Enum
================================================== ==
As part of a Shared class you could have variables like these and a
SortListbox method:

Private Shared mList As New ArrayList
Private Shared i As Integer
Private Shared mItem As ListItem

Public Shared Sub SortListBox(ByRef mylistbox As ListBox, ByVal Ascending
As Boolean, ByVal ListBoxComparer As ListBoxComparer)
mList.Clear()

'We don't want any selected items to exist in the listbox. Problem
exists when more than 1 item is Selected.
mylistbox.ClearSelection()

For Each mItem In mylistbox.Items
mList.Add(mItem)
Next

If ListBoxComparer = ListBoxComparer.String Then
mList.Sort(New ListBoxStringComparer)
ElseIf ListBoxComparer = ListBoxComparer.Decimal Then
mList.Sort(New ListBoxDecimalComparer)
End If

mylistbox.Items.Clear()

If Ascending = True Then
For i = 0 To mList.Count - 1
mylistbox.Items.Add(CType(mList(i), ListItem))
Next
Else
For i = mList.Count - 1 To 0 Step -1
mylistbox.Items.Add(CType(mList(i), ListItem))
Next
End If

End Sub
================================================
These comparer classes can be stored in one of your libraries:

Public Class ListBoxStringComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
'compare 2 ListItem values using their Text property
Return String.Compare(CType(x, ListItem).Text, CType(y,
ListItem).Text)
End Function

End Class
================================================== ==
Public Class ListBoxDecimalComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collections.IComparer.Compare
'compare 2 ListItem values using their Value property
Return Decimal.Compare(CDec(CType(x, ListItem).Value), CDec(CType(y,
ListItem).Value))
End Function

End Class

================================================== ==

--
Joe Fallon


"Jason P Opdycke [MSFT]" <jasonop.online.microsoft.com> wrote in message
news:OF**************@TK2MSFTNGP11.phx.gbl...
Hello All -

I have 2 list boxes. Items in Left list box populated from a DB. I remove an item from the left box and add an item to the right box to allow user
selection. When that item is removed again, the item is in the bottom of
the listbox on the left.

Here is the remove code snippet.... How do I sort to original alphabetical state when the item is removed?

Dim list As New ArrayList

For Each item As ListItem In lstDest.Items

If item.Selected Then

lstSource.Items.Add(item)

list.Add(item)

End If

Next

For Each item As ListItem In list

If item.Selected Then

lstDest.Items.Remove(item)

End If

Next

Thanks to ALL

Nov 18 '05 #2

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

Similar topics

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...
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...
0
by: Ray | last post by:
Folks, I have just created a simple procedure that does the following: Determines the width of the columns of a listbox. Places a button of the correct size above each column as the form opens....
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
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...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
4
by: Just Me | last post by:
I populate a ListBox with Objects (not Strings) that has a String field (which is displayed) and an Integer field (which is not displayed). I need to have the displayed items in the Listbox...
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...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.