473,788 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.R emove(item)

End If

Next

Thanks to ALL
Nov 18 '05 #1
1 2162
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(ByR ef 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.Clear Selection()

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

If ListBoxComparer = ListBoxComparer .String Then
mList.Sort(New ListBoxStringCo mparer)
ElseIf ListBoxComparer = ListBoxComparer .Decimal Then
mList.Sort(New ListBoxDecimalC omparer)
End If

mylistbox.Items .Clear()

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

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

Public Class ListBoxStringCo mparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collecti ons.IComparer.C ompare
'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 ListBoxDecimalC omparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements System.Collecti ons.IComparer.C ompare
'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******** ******@TK2MSFTN GP11.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.R emove(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
5911
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 ArrayList only has one element in it everything works ok. But as soon as I have more than one element, I get the following exception when the control loads up: "Cannot modify the Items collection when the DataSource property is set.". Anybody...
8
8071
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 it with a table and within the table a yes/no field would determine which box it would be in. An Update statement would be fired changing it from yes to no or vice versa and then refreshing the listboxes. This doesn't seem to scale very well as...
0
2306
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. Sorts by that column when you click the button. Automatically toggles between ASC and DESC sort sequence. To implement it on a new form, you simply copy the buttons from my
7
10068
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 there is already one. C.P.
1
2885
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 is added to that column/row. When the admin clicks "update" I am unable to retrieve the values that were selected from that listbox. Basically, This is a multi-select listbox, so I need to loop to pull all selected items. This is the only...
2
2893
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 order? -- Joe Fallon
4
2268
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 sorted. But sorted by the Integer field not the displayed String. Is there some way I can do that (in much the way a ListView can be sorted by any column) Thanks
4
4964
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 listed first followed by the files.
2
1915
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 display only. I'm trying to sort these lists alphabetically A->Z. I have the Sort property set to True. When I run the app, the items in each box are sorted correctly. So far, so good. What I am confused by is that when a category is...
3
2013
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 and refresh the form, or directly sort the listbox. How can i do any of that without having to open the table? I also tried to use a subform that shows the table, that way the user can right click on the column and sort, but the problem is that...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.