|
-
Jan 22nd, 2008, 05:57 PM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] Sorting a Listbox Descending
Hi Guys
In VB 2005 I have a list box which is getting populated during the runtime. How do I show the listbox in descending sort?
Newbie thankx.
-
Jan 22nd, 2008, 06:17 PM
#2
Re: [2005] Sorting a Listbox Descending
A standard ListBox will always sort in ascending order. You have two choices:
1. Define your own class that inherits ListBox and override the Sort method to sort how you want.
2. Set the Sorted property to False and sort the data yourself, e.g.
vb.net Code:
Array.Sort(myArray) Array.Reverse(myArray) myListBox.DataSource = myArray
-
Jan 22nd, 2008, 06:18 PM
#3
Re: [2005] Sorting a Listbox Descending
Setting the listbox' sorted property to true will sort it in an ascending order, but since you need it in a descending order, I suppose you could set Sorted to true, and after adding the items you could do this:
vb Code:
Dim arr(ListBox1.Items.Count - 1) As Object
ListBox1.Items.CopyTo(arr, 0)
Array.Reverse(arr)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(arr)
-
Jan 22nd, 2008, 06:26 PM
#4
Thread Starter
Addicted Member
Re: [2005] Sorting a Listbox Descending
 Originally Posted by Atheist
Setting the listbox' sorted property to true will sort it in an ascending order, but since you need it in a descending order, I suppose you could set Sorted to true, and after adding the items you could do this:
vb Code:
Dim arr(ListBox1.Items.Count - 1) As Object
ListBox1.Items.CopyTo(arr, 0)
Array.Reverse(arr)
ListBox1.Items.Clear()
ListBox1.Items.AddRange(arr)
Thanks this worked nicely.
-
Mar 8th, 2008, 10:30 PM
#5
New Member
Re: [RESOLVED] [2005] Sorting a Listbox Descending
Better solutions is,
ListBox1.Items.Insert(0, "Hi")
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|