|
-
May 6th, 2004, 10:50 AM
#1
Thread Starter
Hyperactive Member
Listbox Sorting - Numbers *[NOW RESOLVED]*
How do you sort numbers in a listbox? I have the sorted property to true but the numbers come out like:
102
21
231
34
536
55
instead of
21
34
55
102
231
536
How do I sort these into correct numerical order?
Last edited by RealNickyDude; May 8th, 2004 at 06:02 AM.
-
May 6th, 2004, 11:26 AM
#2
Are you adding them as strings, or as numbers?
There's a difference between adding them like this:
ListBox1.Items.Add("112")
and
ListBox1.Items.Add(112)
-
May 6th, 2004, 12:24 PM
#3
Thread Starter
Hyperactive Member
I added the numbers through the Items - (collection) Property, this must add them as strings.
Then I tried
Code:
With lstSectionList
.Items.Add("<NEW>")
.Items.Add(112)
.Items.Add(32)
.Items.Add(102)
.Items.Add(11)
.Items.Add(53)
End With
As there's a mixture of string and numbers, and it came out as:
102
11
112
32
53
Not the way I wanted them sorted. Any more help?
-
May 6th, 2004, 12:57 PM
#4
I must be going crazy, I ran an example when I saw your question first, and I could have sworn it worked for me.
Looks like...You'll have to write a subroutine for this.
-
May 6th, 2004, 12:58 PM
#5
The easiest way to do this would be to format the numbers, so that you have leading zeros on the numbers. Like so:
VB Code:
ListBox1.Items.Add(Format(5, "00000#"))
ListBox1.Items.Add(Format(1125, "00000#"))
ListBox1.Items.Add(Format(865, "00000#"))
-
May 6th, 2004, 01:03 PM
#6
Thread Starter
Hyperactive Member
Originally posted by Negative0
The easiest way to do this would be to format the numbers, so that you have leading zeros on the numbers. Like so:
VB Code:
ListBox1.Items.Add(Format(5, "00000#"))
ListBox1.Items.Add(Format(1125, "00000#"))
ListBox1.Items.Add(Format(865, "00000#"))
That worked, thanks. but is there anyway to sort the numbers without having the leading zero's?
-
May 6th, 2004, 01:08 PM
#7
See if this helps, I wrote it right now, so it might be buggy.
VB Code:
Sub SortListBox(ByRef box As ListBox)
Dim myarray(box.Items.Count) As Integer
Dim i As Integer
Dim j As Integer = 0
Dim intTemp As Integer
For i = 0 To box.Items.Count - 1
myarray(i) = CInt(box.Items(i))
Next
myarray.Sort(myarray)
i = 0
Do While i < UBound(myarray)
Do While j < UBound(myarray) - i
If CInt(myarray(j + 1)) < CInt(myarray(j)) Then
intTemp = CInt(myarray(j))
myarray(j) = CInt(myarray(j + 1))
myarray(j + 1) = CInt(intTemp)
End If
j += 1
Loop
j = 0
i += 1
Loop
' array1.Sort(i)
box.Items.Clear()
For i = UBound(myarray) To 0 Step -1
box.Items.Add(myarray(i))
Next
End Sub
-
May 6th, 2004, 01:09 PM
#8
You would have to have the sorted property set to false and sort the list manually. Then insert the items from your sort.
-
May 6th, 2004, 01:56 PM
#9
Thread Starter
Hyperactive Member
Originally posted by mendhak
See if this helps, I wrote it right now, so it might be buggy.
VB Code:
Sub SortListBox(ByRef box As ListBox)
Dim myarray(box.Items.Count) As Integer
Dim i As Integer
Dim j As Integer = 0
Dim intTemp As Integer
For i = 0 To box.Items.Count - 1
myarray(i) = CInt(box.Items(i))
Next
myarray.Sort(myarray)
i = 0
Do While i < UBound(myarray)
Do While j < UBound(myarray) - i
If CInt(myarray(j + 1)) < CInt(myarray(j)) Then
intTemp = CInt(myarray(j))
myarray(j) = CInt(myarray(j + 1))
myarray(j + 1) = CInt(intTemp)
End If
j += 1
Loop
j = 0
i += 1
Loop
' array1.Sort(i)
box.Items.Clear()
For i = UBound(myarray) To 0 Step -1
box.Items.Add(myarray(i))
Next
End Sub
Being a newbi, this is a bit over my head so I think I'll stick with the '00' format. Thanks.
-
May 6th, 2004, 02:03 PM
#10
Originally posted by RealNickyDude
Being a newbi, this is a bit over my head so I think I'll stick with the '00' format. Thanks.
Uhm... it's very simple code, working with arrays. Just the bubble sort. I'm a newbie too, sorry if that was too difficult though. I'm with you on the VB.NET learning path!
-
May 6th, 2004, 02:30 PM
#11
No need to write your own sort:
Code:
Dim boxArr As New ArrayList
boxArr.Add(15)
boxArr.Add(125)
boxArr.Add(715)
boxArr.Add(1)
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
Just make sure the sorted property on the listbox is false.
-
May 6th, 2004, 04:54 PM
#12
Thread Starter
Hyperactive Member
Thanks, that seemed to work ok.
-
May 8th, 2004, 03:15 AM
#13
Thread Starter
Hyperactive Member
Well, I thought it worked
Here's my code for adding the text from a textbox
VB Code:
Private Sub txtAddSection_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Handles txtAddSection.KeyDown
If e.KeyCode = 13 Then
boxArr.Add(txtAddSection.Text) 'Add text into the array
ListSort() 'Sort the array
txtAddSection.Clear() 'Clear text ready for next one
End If
End Sub
and here's the sort
VB Code:
Private Sub ListSort()
' This sorts the Section numbers into the correct order
' and places the result in the listbox.
boxArr.Sort()
Dim i As Int16
lstSectionList.Items.Clear() 'Clear list
For i = 0 To boxArr.Count - 1
'lstSectionList.Items.Add(Format(boxArr(i), "00#"))
lstSectionList.Items.Add(boxArr(i))
Next
End Sub
but instead of
4
12
25
33
88
100
300
it comes out as
100
12
25
300
33
4
88
I tried formatting the output
Code:
lstSectionList.Items.Add(Format(boxArr(i), "00#"))
but all that comes out, no matter what number I type in is 00#.
Last edited by RealNickyDude; May 8th, 2004 at 05:28 PM.
-
May 8th, 2004, 05:12 AM
#14
PowerPoster
Hi,
"Are you adding them as strings, or as numbers?
There's a difference between adding them like this:
ListBox1.Items.Add("112")
and
ListBox1.Items.Add(112)"
Surely anything added to a listbox or a combobox is accepted as a string??? Therefore, a numerical sort is not possible. The data must be sorted numerically before adding to the box.
Negative's posted code DOES work.
" Dim boxArr As New ArrayList
boxArr.Add(15)
boxArr.Add(125)
boxArr.Add(715)
boxArr.Add(1)
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next"
BUT YOU MUST SET THE LISTBOX SORTED PROPERTY TO FALSE
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 8th, 2004, 05:36 AM
#15
Thread Starter
Hyperactive Member
Thanks for the reply Taxes,
The Sorted property is set to false
I think i'm getting it now, I'm adding the numbers through a text box,
Code:
boxArr.Add(txtAddSection.Text) 'Add text into the array
so I guess this is adding the numbers as strings. I need to convert the number from a textbox to an integer before adding it to the array.
I've now tried
Code:
Convert.ToInt16(txtAddSection.Text)
but to no avail, how do I convert a textbox string to an integer?
-
May 8th, 2004, 05:44 AM
#16
PowerPoster
Hi,
Presumeably you are using one text box into which you insert the number and then use a button to accept the entry.
In the button click event
boxArr.add(val(txtBox.text)
then when ready
boxArr.Sort()
Dim i As Int16
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 8th, 2004, 05:59 AM
#17
Thread Starter
Hyperactive Member
Val, of course, doh!
Yup, all works correctly now, thanks for taking the time to help a newbie
-
May 8th, 2004, 06:00 AM
#18
Addicted Member
Hi,
Dim boxArr As New ArrayList()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = 13 Then
boxArr.Add(Val((TextBox1.Text))) 'Add text into the array
ListSort() 'Sort the array
TextBox1.Clear() 'Clear text ready for next one
End If
End Sub
Private Sub ListSort()
boxArr.Sort()
Dim i As Int16
ListBox1.Items.Clear() 'Clear list
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
End Sub
Have a nice day
-
May 8th, 2004, 06:03 AM
#19
Thread Starter
Hyperactive Member
Originally posted by yulyos
Hi,
Dim boxArr As New ArrayList()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = 13 Then
boxArr.Add(Val((TextBox1.Text))) 'Add text into the array
ListSort() 'Sort the array
TextBox1.Clear() 'Clear text ready for next one
End If
End Sub
Private Sub ListSort()
boxArr.Sort()
Dim i As Int16
ListBox1.Items.Clear() 'Clear list
For i = 0 To boxArr.Count - 1
ListBox1.Items.Add(boxArr(i))
Next
End Sub
Have a nice day
Exactly what have yulyos
-
May 8th, 2004, 06:26 AM
#20
Addicted Member
Hi,
Look at the time we post the reply
Have a nice day
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
|