Webform - Create SortedListbox Control - need help!

  • Thread starter Thread starter James Radke
  • Start date Start date
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 (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
 
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
 
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.
 
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
 
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" <[email protected]>
| From: "James Radke" <[email protected]>
| References: <[email protected]>
<[email protected]>
| 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: <#[email protected]>
| 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!TK2MSFTNGP10.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
|
| | > 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
| >
| >
|
|
|
 
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
 
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
 
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" <[email protected]>
| From: "James Radke" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
| 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: <[email protected]>
| 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!tk2msftngp13.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
|
| | > 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
| >
| >
|
|
|
 
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.
 
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
 
Hi James,

Composition is a disign pattern which means to wrap an object as a member
in your own class. To achieve this, you can create a user control in your
application. Please right click your web application in the solution
explorer, and choose Add Web User Control in the popup menu. Name your user
control and click open. A web user control is created. It is interited from
the UserControl. Now you can try the folloing codes:

Public Class WebUserControl1
Inherits System.Web.UI.UserControl 'Auto Generated

Public _listbox As ListBox 'You can use Public or Private as you
wish

Public Sub add(ByVal s As String)
Me._listbox.Items.Add(s)
Me.Sort()
End Sub

Private Sub Sort()
' Your sort method. It is private.
End Sub

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" <[email protected]>
| From: "James Radke" <[email protected]>
| References: <[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<h#[email protected]>
| Subject: Re: Create SortedListbox Control - need help! (Typical for
Fergus I think)
| Date: Fri, 26 Sep 2003 10:06:02 -0500
| Lines: 93
| 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.languages.vb
| NNTP-Posting-Host: cpe-24-167-241-101.wi.rr.com 24.167.241.101
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:141560
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| 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
|
| | > 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" <[email protected]>
| > | From: "James Radke" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > <#[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | 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: <[email protected]>
| > | 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!tk2msftngp13.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
| > |
| > | | > | > 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
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
Back
Top