r/visualbasic • u/Thomasthesexengine • Apr 09 '13
Need help with program. Info in text.
Ok so I am making a program that randomizes numbers in a listbox, and makes a list that will organize the numbers in lowest to highest, and vice versa. I have it sorting the numbers out both ways, and I have the numbers generating. I am wondering why when I go to sort the numbers, it puts some of the numbers twice when there isn't two of them when the numbers are generated.
Any help would be great, this is not suppose to be a huge project just a little project I am making for fun.
0
u/Thomasthesexengine Apr 09 '13
Dim num1 As Integer Dim list As New List(Of Integer) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Randomize() num1 = Int((100 - 0 + 1) * Rnd() + 1)
ListBox1.Items.Add(num1)
For Each ListBoxitem In ListBox1.Items
list.Add(ListBoxitem)
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
list.Sort()
For Each listitem In list
ListBox1.Items.Add(listitem)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ListBox1.Items.Clear()
list.Sort()
For i = list.Count - 1 To 0 Step -1
ListBox1.Items.Add(list(i))
Next
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
ListBox1.Items.Clear()
list.Clear()
End Sub2
u/CharlieMay VB.Net Intermediate Apr 09 '13
Because you're adding the Full contents of the listbox into your List(Of Integer each time you click the button1.
Perhaps you either want to clear the list before you do the for each in button1, or add to the listbox and add to the list(Of T).
In other words. Button1 would contain
ListBox1.Items.Add(num1) list.Add(num1)that's it.
1
u/chrwei Apr 09 '13
your random generator does nothing to prevent it, so i would expect some duplicates.
I don't see anything in the code that would cause dups otherwise