J
James Radke
Hello,
I would like to create a sorted listbox which has one property that I can
set to true or false to show if the listbox should be sorted. Then, if it
should be sorted, everytime an item is added to the list via Items.Add or or
deleted via items.remove, I would like it to automatically resort the list.
I have created the base class below... but this only allows me to manually
run the SORT routine. I would prefer that this be a private sub, and be
called automatically when items are added or deleted ( note: databound
lists would have to be sorted via the SQL used reading the database).
Can this be done? Can someone help me out with how?
Thanks!
Jim
================================ Start of SortedListBox class
=====================================================
Public Class SortedListBox
Inherits System.Web.UI.WebControls.ListBox
Private _sorted As Boolean
Public Property Sorted() As Boolean
Get
Return _sorted
End Get
Set(ByVal Value As Boolean)
_sorted = Value
End Set
End Property
Public Sub Sort()
' Declare private variables needed to get the sort to work.
Dim x As New Collection
Dim Ar As New ArrayList
Dim li As ListItem
Dim txtString As String
' Add the text to a sortable object
For Each li In Items
Ar.Add(li.Text.ToString)
Next
' Sort the list of objects
Ar.Sort()
' Create a new list of items
For Each txtString In Ar
Dim templi As New ListItem
templi.Text = txtString
templi.Value = Items.FindByText(txtString).Value
x.Add(templi)
Next
' Clear all the items from the listbox
Items.Clear()
' Add back in the sorted items
For Each li In x
Items.Add(li)
Next
End Sub
End Class
I would like to create a sorted listbox which has one property that I can
set to true or false to show if the listbox should be sorted. Then, if it
should be sorted, everytime an item is added to the list via Items.Add or or
deleted via items.remove, I would like it to automatically resort the list.
I have created the base class below... but this only allows me to manually
run the SORT routine. I would prefer that this be a private sub, and be
called automatically when items are added or deleted ( note: databound
lists would have to be sorted via the SQL used reading the database).
Can this be done? Can someone help me out with how?
Thanks!
Jim
================================ Start of SortedListBox class
=====================================================
Public Class SortedListBox
Inherits System.Web.UI.WebControls.ListBox
Private _sorted As Boolean
Public Property Sorted() As Boolean
Get
Return _sorted
End Get
Set(ByVal Value As Boolean)
_sorted = Value
End Set
End Property
Public Sub Sort()
' Declare private variables needed to get the sort to work.
Dim x As New Collection
Dim Ar As New ArrayList
Dim li As ListItem
Dim txtString As String
' Add the text to a sortable object
For Each li In Items
Ar.Add(li.Text.ToString)
Next
' Sort the list of objects
Ar.Sort()
' Create a new list of items
For Each txtString In Ar
Dim templi As New ListItem
templi.Text = txtString
templi.Value = Items.FindByText(txtString).Value
x.Add(templi)
Next
' Clear all the items from the listbox
Items.Clear()
' Add back in the sorted items
For Each li In x
Items.Add(li)
Next
End Sub
End Class