473,773 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the best way to sort a Web Forms Listbox as new items are added?

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

Nov 20 '05 #1
2 2892
I came up with this generic routine that sorts a listbox Asc or Desc:

Private mList As New ArrayList

Private i As Integer

Private mItem As ListItem

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

Private Sub SortListBox(ByR ef mylistbox As ListBox, ByVal Ascending As
Boolean)

mList.Clear()

For Each mItem In mylistbox.Items

mList.Add(mItem .Text)

Next

mList.Sort()

mylistbox.Items .Clear()

If Ascending = True Then

For i = 0 To mList.Count - 1

mylistbox.Items .Add(CStr(mList (i)))

Next

Else

For i = mList.Count - 1 To 0 Step -1

mylistbox.Items .Add(CStr(mList (i)))

Next

End If

End Sub
--
Joe Fallon

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:eR******** ******@TK2MSFTN GP12.phx.gbl...
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


Nov 20 '05 #2
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:eH******** ********@TK2MSF TNGP09.phx.gbl. ..
I came up with this generic routine that sorts a listbox Asc or Desc:


Hi Joe,

Your code worked of course but I noted a couple of things... couldn't
exactly figure out why mList and mItem should be private (they can be
declared in SortListBox) and such. Here is another way to approach the
solution however.

Note that I let the sort routine handle the sorting :-) You had it sorting
but then gathered up the items in reverse order to affect a descending sort.
By intercepting the sorting algorithm you can affect the sort in any manner.
Implementing case insensitive sorts of even sorting on some substring of the
text if desired.

I named mine "ListSort" but this is your SortListBox" changed a bit. It
takes a "Sorter" parameter instead of a boolean which represents the class
(and therefore the code) used to perform the sort. As a result the
reassignment to the listbox is identical regardless of which way the items
are sorted.

You call it this way:

ListSort(lst1, New AscendSorter)
or
ListSort(lst1, New DescendSorter)

and of course you can implement other sorter classes and simply pass them
along also.
Private Sub ListSort(ByRef lbox As ListBox, ByVal Sorter As IComparer)

Dim mList As New ArrayList

With mList

Dim mItem As ListItem

For Each mItem In lbox.Items
.Add(mItem.Text )
Next

.Sort(Sorter)

End With

With lbox

.Items.Clear()

Dim i As Integer
For i = 0 To (mList.Count - 1)
.Items.Add(CStr (mList(i)))
Next

End With

End Sub
Private Class AscendSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collecti ons.IComparer.C ompare

Dim xx As String = CType(x, String)
Dim yy As String = CType(y, String)

If xx > yy Then
Compare = 1
Else
If xx < yy Then
Compare = -1
Else
Compare = 0
End If
End If

End Function

End Class

Private Class DescendSorter
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collecti ons.IComparer.C ompare

Dim xx As String = CType(x, String)
Dim yy As String = CType(y, String)

If yy > xx Then
Compare = 1
Else
If yy < xx Then
Compare = -1
Else
Compare = 0
End If
End If

End Function

End Class
Nov 20 '05 #3

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

Similar topics

6
6507
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the page does not know the items were added client-side.) TIA, Val
1
2161
by: Jason P Opdycke [MSFT] | last post by:
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?
4
13440
by: benoit | last post by:
Hi, I have this ListBox in which the Itemciollection can be manipulated by the user (items can be added one way or another, and moved to other listboxes). Is there any way that I can get this collection to be sorted alphabetically? Thx
4
18210
by: Robin Tucker | last post by:
How do I sort the items in a list box? I am using a class derived from IComparer to sort items on columns in a ListView, but the ListBox doesn't support this kind of facility. The "items" in my list box aren't just strings, they are a class I have defined for each list item. There is a comparitor function, but how can I write - ListBox.Items.Sort() in this instance? Do I need to do my own quicksort?
1
1429
by: Dib | last post by:
Hi, how do I call a form to open from another form. example I have a frmLogIn and an frmError The frmError have a ListBox. If the frmLogin generated an error I want the error to show in the ListBox on frmError.
4
5042
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results of computations, etc). when all of the calculations are complete, I'm trying to change the listbox selection mode from single to MultiExtended, using: myListBox.SelectionMode=SelectionMode.MultiExtended
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.
6
3964
by: Zytan | last post by:
In win32 there was a way to 'turn off' a listbox from updating until you were done adding elements. Is there a way to do this in .NET? Zytan
0
1752
by: anelie | last post by:
the problem is: using 2 listboxes and 2 command buttons.. when you click the 1st cmd button an inputbox will appear asking the number of items to be sorted..then when you inputed a number, another inputboxes will appear asking for the items to be sorted... then the items you inputed will be printed in the 1st listbox.. then when you click the 2nd cmd button the items in the first listbox will appear in the second listbox but they...
0
9621
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6717
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
5355
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...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
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.