Creating SortedListBox control, but need a little help!

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
 
J

Jeffrey Tan[MSFT]

Hi James,

In web form, your item will be added into/removed from your listbox control
by postback.
So you can call the sort method in the listbox's load event.
The data binding also can be sorted in the load event.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Reply-To: "James Radke" <[email protected]>
| From: "James Radke" <[email protected]>
| Subject: Creating SortedListBox control, but need a little help!
| Date: Fri, 19 Sep 2003 20:32:44 -0500
| Lines: 67
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:14788
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| 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
|
|
|
 
J

James Radke

Jeffrey,

Wouldn't I lose information if I always did it in the load event, like if I
don't happen to be adding or deleting an item, and if an item is selected,
wouldn't I lose the selection and perhaps other things like that?

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,567
Members
47,204
Latest member
abhinav72673

Latest Threads

Top