By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

Listbox - How do I sort?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.