473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Sort ListBox

Can the items in a ListBox be sorted by the name of the items?

The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.

Jan 10 '07 #1
4 4941
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
Can the items in a ListBox be sorted by the name of the items?

The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.

Jan 10 '07 #2
David, this is how I am adding the directories & files in the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>", fsi.Name
& "+F"))
End If
Next
SortListItems()
End Sub

Sub SortListItems()
Dim lItem As ListItem
Dim aList As ArrayList

aList = New ArrayList(lstFD.Items.Count)
For Each lItem In lstFD.Items
aList.Add(lItem)
Next

aList.Sort()
End Sub

Now how do I add the sorted ArrayList to the ListBox?

Note that the values of the items in the ListBox will not be exactly
the same as the text of the items. The text of each item will be the
directory name &/or file name but the value of each item will be the
directory name followed by the string '+D' &/or file name followed by
the string '+F' (both without quotes). In other words, if an item in
the ListBox is a directory, the value of that item will be the
directory name followed by '+D'. Similarly, if an item in the ListBox
is a file, its corresponding value will be the file name followed by
'+F'. I have done it this way so that I can easily differentiate which
item is a directory & which item is a file & then take action
accordingly when the Form submits.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
Can the items in a ListBox be sorted by the name of the items?

The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.
Jan 10 '07 #3
David, this is the revised code:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
'lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
'lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>",
fsi.Name & "+F"))
End If
aList.Add(fsi.Name)
Next

aList.Sort()

lstFD.DataSource = aList
lstFD.DataBind()
End Sub

The above code does sort the ListBox but as I had said, I would like to
first list all the directories & then list the files in the ListBox.

Moreover, using the above code, the value of each item remains exactly
the same as the text of the item in the ListBox but I want the values
of the items which are directories to be the directory names followed
by '+D'. Similarly, the values of the items which are files should be
the file names followed by '+F'.

What changes need I incorporate to implement these 2 features?

r...@rediffmail.com wrote:
David, this is how I am adding the directories & files in the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>", fsi.Name
& "+F"))
End If
Next
SortListItems()
End Sub

Sub SortListItems()
Dim lItem As ListItem
Dim aList As ArrayList

aList = New ArrayList(lstFD.Items.Count)
For Each lItem In lstFD.Items
aList.Add(lItem)
Next

aList.Sort()
End Sub

Now how do I add the sorted ArrayList to the ListBox?

Note that the values of the items in the ListBox will not be exactly
the same as the text of the items. The text of each item will be the
directory name &/or file name but the value of each item will be the
directory name followed by the string '+D' &/or file name followed by
the string '+F' (both without quotes). In other words, if an item in
the ListBox is a directory, the value of that item will be the
directory name followed by '+D'. Similarly, if an item in the ListBox
is a file, its corresponding value will be the file name followed by
'+F'. I have done it this way so that I can easily differentiate which
item is a directory & which item is a file & then take action
accordingly when the Form submits.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com

<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
Can the items in a ListBox be sorted by the name of the items?
>
The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.
>
Jan 10 '07 #4
David, I did it at last. This is the code:

Sub Page_Load(.....)
Dim i As Integer
Dim aList As ArrayList
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo
Dim strListItemValue As String

aList = New ArrayList
dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
'precede all directory names with 0
'so that they get listed before the files
aList.Add("0" & fsi.Name & "<DIR>")
Else
'precede all file names with 1 so that
'that they get listed after the directories
aList.Add("1" & fsi.Name & "<FILE>")
End If
Next

aList.Sort()

For i = 0 To aList.Count - 1
If (InStr(aList(i), "<DIR>") 0) Then
'this item is a directory
strListItemValue = Left(aList(i), Len(aList(i)) - 5)
strListItemValue = Mid(strListItemValue, 2,
Len(strListItemValue)) & "+D"
Else
'this item is a file
strListItemValue = Left(aList(i), Len(aList(i)) - 6)
strListItemValue = Mid(strListItemValue, 2,
Len(strListItemValue)) & "+F"
End If

'get rid of the 0s & 1s preceding all the
'ListItems to display the file/directory name
lstFD.Items.Add(New ListItem(Mid(aList(i), 2, Len(aList(i))),
strListItemValue))
Next
End Sub
r...@rediffmail.com wrote:
David, this is the revised code:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
'lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
'lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>",
fsi.Name & "+F"))
End If
aList.Add(fsi.Name)
Next

aList.Sort()

lstFD.DataSource = aList
lstFD.DataBind()
End Sub

The above code does sort the ListBox but as I had said, I would like to
first list all the directories & then list the files in the ListBox.

Moreover, using the above code, the value of each item remains exactly
the same as the text of the item in the ListBox but I want the values
of the items which are directories to be the directory names followed
by '+D'. Similarly, the values of the items which are files should be
the file names followed by '+F'.

What changes need I incorporate to implement these 2 features?

r...@rediffmail.com wrote:
David, this is how I am adding the directories & files in the ListBox:

Sub Page_Load(.....)
Dim fsi As FileSystemInfo
Dim dInfo As DirectoryInfo

dInfo = New DirectoryInfo(Server.MapPath("Folder1"))
For Each fsi In dInfo.GetFileSystemInfos
If ((fsi.Attributes And FileAttributes.Directory) = 16) Then
lstFD.Items.Add(New ListItem(fsi.Name & " <DIR>", fsi.Name
& "+D"))
Else
lstFD.Items.Add(New ListItem(fsi.Name & " <FILE>", fsi.Name
& "+F"))
End If
Next
SortListItems()
End Sub

Sub SortListItems()
Dim lItem As ListItem
Dim aList As ArrayList

aList = New ArrayList(lstFD.Items.Count)
For Each lItem In lstFD.Items
aList.Add(lItem)
Next

aList.Sort()
End Sub

Now how do I add the sorted ArrayList to the ListBox?

Note that the values of the items in the ListBox will not be exactly
the same as the text of the items. The text of each item will be the
directory name &/or file name but the value of each item will be the
directory name followed by the string '+D' &/or file name followed by
the string '+F' (both without quotes). In other words, if an item in
the ListBox is a directory, the value of that item will be the
directory name followed by '+D'. Similarly, if an item in the ListBox
is a file, its corresponding value will be the file name followed by
'+F'. I have done it this way so that I can easily differentiate which
item is a directory & which item is a file & then take action
accordingly when the Form submits.
David Wier wrote:
Not directly, though, if you add the items to an ArrayList first, and then
bind it to the Listbox, the arraylist is sort-able
Here's a code sample from ASPNet101.com:
http://aspnet101.com/aspnet101/aspne...code=arraylist
>
--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
>
>
>
<rn**@rediffmail.comwrote in message
news:11**********************@i39g2000hsf.googlegr oups.com...
Can the items in a ListBox be sorted by the name of the items?

The ListBox actually lists all directories & files existing in a
directory on the server. Note that all the directories should be listed
first followed by the files.
Jan 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
0
by: Ray | last post by:
Folks, I have just created a simple procedure that does the following: Determines the width of the columns of a listbox. Places a button of the correct size above each column as the form opens....
1
by: Jason P Opdycke [MSFT] | last post by:
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...
4
by: Robin Tucker | last post by:
How do I sort the items in a list box? I am using a class derived from IComparer to sort items on columns in a ListView, but the ListBox doesn't support this kind of facility. The "items" in my...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
4
by: Just Me | last post by:
I populate a ListBox with Objects (not Strings) that has a String field (which is displayed) and an Integer field (which is not displayed). I need to have the displayed items in the Listbox...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
0
by: anelie | last post by:
the problem is: using 2 listboxes and 2 command buttons.. when you click the 1st cmd button an inputbox will appear asking the number of items to be sorted..then when you inputed a number,...
3
by: RamiH | last post by:
Hi, I am using a listbox to show data from a certain table, i want the user, after the listbox is filled, to be able to sort the data according to the column of his choice. I could sort the table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.