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

Webform - Create SortedListbox Control - need help!

P: n/a
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 (I may
potentially
add another one for the sort direction - asc, or desc). 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 public SORT method. 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?

Below I have the new class as I have it started now....

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

Nov 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Cor
Hi James,

I don't know if I can help you, but for I can do that, I need something to
know.
How do you fill your listbox.
And how the deletes and the adds.

Cor
Nov 20 '05 #2

P: n/a
Cor
Hi Fergus,
First I thought (still is) this is typical yours, but then I thought lets
first try to get some information.
Maybe I can do it too. I forgot to set the subject again back.
Sorry
Cor
ps. when you think you can answer this, do it otherwise I do it tomorrow
morning (GMT) , I wrote this to overcome that there is a "go to ASP.NET" and
this has again nothing to with that but is deep 100% VB.net language.
Nov 20 '05 #3

P: n/a
Cor,

I simply do the listitem.add method, or listitem.delete method.

Think of it as a web page where you show two listboxes, one on the left with
all options for a user, and one on the right showing options selected for
the user (both of which we want sorted for ease of viewing - even as they
add/move from listbox to listbox). We let them move the items between the
two listboxes by using arrows in between them. Then, whenever they want
they can press the Apply button, which makes the changes permanent to the
database.

Does that help?

Jim

"Cor" <no*@non.com> wrote in message
news:3f**********************@reader21.wxs.nl...
Hi James,

I don't know if I can help you, but for I can do that, I need something to
know.
How do you fill your listbox.
And how the deletes and the adds.

Cor

Nov 20 '05 #4

P: n/a
Hi James,

I think you can handle the Load event of your control to achieve this. When
you press the button to add an item to them listbox, IE posts back the page
to the server and the whole page is reloaded. This event fires every time
when your control is loaded. In the Load event, you can call your private
sub Sort() to sort the the items.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Reply-To: "James Radke" <jr*****@wi.rr.com>
| From: "James Radke" <jr*****@wi.rr.com>
| References: <uM**************@TK2MSFTNGP12.phx.gbl>
<3f**********************@reader21.wxs.nl>
| Subject: Re: Create SortedListbox Control - need help! (Typical for
Fergus I think)
| Date: Tue, 23 Sep 2003 15:40:59 -0500
| Lines: 30
| 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: <#y**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:140716
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Cor,
|
| I simply do the listitem.add method, or listitem.delete method.
|
| Think of it as a web page where you show two listboxes, one on the left
with
| all options for a user, and one on the right showing options selected for
| the user (both of which we want sorted for ease of viewing - even as they
| add/move from listbox to listbox). We let them move the items between the
| two listboxes by using arrows in between them. Then, whenever they want
| they can press the Apply button, which makes the changes permanent to the
| database.
|
| Does that help?
|
| Jim
|
| "Cor" <no*@non.com> wrote in message
| news:3f**********************@reader21.wxs.nl...
| > Hi James,
| >
| > I don't know if I can help you, but for I can do that, I need something
to
| > know.
| > How do you fill your listbox.
| > And how the deletes and the adds.
| >
| > Cor
| >
| >
|
|
|

Nov 20 '05 #5

P: n/a
Cor
Hi James,
I just started at your problem when I saw Kevin had given you an anwser.
What I will add to Kevins answer is,
The Load event on the serverside will be started when a webcontrol button or
a webcontrol with the attribute postback=true is clicked.
(Your from and too buttons)
When I was you, I would first test what that means on a real Internet
connection.
I can imaging that it takes a lot of traffic when the user does this kind of
operations on the serverside.
Or maybe Kevin knows that,
Cor
Nov 20 '05 #6

P: n/a
The only thing I am worried about in placing it in the load event, is that
it would also then happen if I happen to have the control databound, right?
Is there a way I can identify if it is databound, and if so, NOT do the
sort?

And, would the LOAD occurr before, or after the item.add/item.delete in a
postback event?

Jim

"Cor" <no*@non.com> wrote in message
news:3f***********************@reader22.wxs.nl...
Hi James,
I just started at your problem when I saw Kevin had given you an anwser.
What I will add to Kevins answer is,
The Load event on the serverside will be started when a webcontrol button or a webcontrol with the attribute postback=true is clicked.
(Your from and too buttons)
When I was you, I would first test what that means on a real Internet
connection.
I can imaging that it takes a lot of traffic when the user does this kind of operations on the serverside.
Or maybe Kevin knows that,
Cor

Nov 20 '05 #7

P: n/a
Hi James,

You're right. The Load will occurr before the button_click event fires.
Apologize for my mistake.

Since the ListBox doesn't have an event which fires when the item.add() is
called, I recommend you to use composition instead of inherit. That is the
ListBox is a member of your user control. In the add() method of your own
control, call ListBox.Items.Add() first, and then call your Sort() method.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Reply-To: "James Radke" <jr*****@wi.rr.com>
| From: "James Radke" <jr*****@wi.rr.com>
| References: <uM**************@TK2MSFTNGP12.phx.gbl>
<3f**********************@reader21.wxs.nl>
<#y**************@TK2MSFTNGP10.phx.gbl>
<hb**************@cpmsftngxa06.phx.gbl>
<3f***********************@reader22.wxs.nl>
| Subject: Re: Create SortedListbox Control - need help! (Typical for
Fergus I think)
| Date: Wed, 24 Sep 2003 11:21:11 -0500
| Lines: 30
| 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: <u7*************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:140948
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| The only thing I am worried about in placing it in the load event, is that
| it would also then happen if I happen to have the control databound,
right?
| Is there a way I can identify if it is databound, and if so, NOT do the
| sort?
|
| And, would the LOAD occurr before, or after the item.add/item.delete in a
| postback event?
|
| Jim
|
| "Cor" <no*@non.com> wrote in message
| news:3f***********************@reader22.wxs.nl...
| > Hi James,
| > I just started at your problem when I saw Kevin had given you an anwser.
| > What I will add to Kevins answer is,
| > The Load event on the serverside will be started when a webcontrol
button
| or
| > a webcontrol with the attribute postback=true is clicked.
| > (Your from and too buttons)
| > When I was you, I would first test what that means on a real Internet
| > connection.
| > I can imaging that it takes a lot of traffic when the user does this
kind
| of
| > operations on the serverside.
| > Or maybe Kevin knows that,
| > Cor
| >
| >
|
|
|

Nov 20 '05 #8

P: n/a
Cor
Hi James,

I was thinking that the meaning of your code was a model for something as
Public function (byvalue as listbox) as listbox
And then in the load of the page (or every other place before the page is
sended back)
xs as new sortedlistbox
listboxa = xs.sort(listboxa)

But now I think I see it wrong?

Cor

ps. I don't know if this works, I did not test it.
Nov 20 '05 #9

P: n/a
Kevin,

So, would I still create a class which 'inherits' from the listbox? I
guess I don't understand what you mean by 'composition', do you have a small
example? Would that still make all the methods/properties of the listbox
available through it?

Thanks!

Jim

"Kevin Yu" <v-****@online.microsoft.com> wrote in message
news:h%****************@cpmsftngxa06.phx.gbl...
Hi James,

You're right. The Load will occurr before the button_click event fires.
Apologize for my mistake.

Since the ListBox doesn't have an event which fires when the item.add() is
called, I recommend you to use composition instead of inherit. That is the
ListBox is a member of your user control. In the add() method of your own
control, call ListBox.Items.Add() first, and then call your Sort() method.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| Reply-To: "James Radke" <jr*****@wi.rr.com>
| From: "James Radke" <jr*****@wi.rr.com>
| References: <uM**************@TK2MSFTNGP12.phx.gbl>
<3f**********************@reader21.wxs.nl>
<#y**************@TK2MSFTNGP10.phx.gbl>
<hb**************@cpmsftngxa06.phx.gbl>
<3f***********************@reader22.wxs.nl>
| Subject: Re: Create SortedListbox Control - need help! (Typical for
Fergus I think)
| Date: Wed, 24 Sep 2003 11:21:11 -0500
| Lines: 30
| 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: <u7*************@tk2msftngp13.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:140948
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| The only thing I am worried about in placing it in the load event, is that | it would also then happen if I happen to have the control databound,
right?
| Is there a way I can identify if it is databound, and if so, NOT do the
| sort?
|
| And, would the LOAD occurr before, or after the item.add/item.delete in a | postback event?
|
| Jim
|
| "Cor" <no*@non.com> wrote in message
| news:3f***********************@reader22.wxs.nl...
| > Hi James,
| > I just started at your problem when I saw Kevin had given you an anwser. | > What I will add to Kevins answer is,
| > The Load event on the serverside will be started when a webcontrol
button
| or
| > a webcontrol with the attribute postback=true is clicked.
| > (Your from and too buttons)
| > When I was you, I would first test what that means on a real Internet
| > connection.
| > I can imaging that it takes a lot of traffic when the user does this
kind
| of
| > operations on the serverside.
| > Or maybe Kevin knows that,
| > Cor
| >
| >
|
|
|

Nov 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.