|
-
Jun 12th, 2011, 08:06 AM
#1
Thread Starter
Lively Member
[RESOLVED] Random from one listbox to another
I have two listboxes and one text box. lbxData is already populated with strings (ex; car, boat, motorcycle) I have a text box (tbxRandom) for user input to choose the number of random strings they want generated and placed in the other listbox (lbxRandom) when clicking btnRandom. Here is what I have but keep getting an error. mylist.addrange(str) "value cannot be null. parameter name: collection
Code:
Dim Str As String
'Declare a List
Dim myList As New List(Of String)
'Mylist gets the values of the existing array
myList.AddRange(Str)
'Declare random
Dim rnd As New System.Random
'This holds the generated random number
Dim randomNumber As Integer = 0
Me.lbxRandom.Items.Clear()
Dim count As Integer = 0
'textbox for user to choose the number of randomly generated items
If Integer.TryParse(tbxRandom.Text, count) Then
'Keep looping until all items generated
Do While count <> 0
'Generate randomNumber
randomNumber = rnd.Next(0, myList.Count)
'Add current item to listbox
Me.lbxRandom.Items.Add(myList(randomNumber))
'Remove the used item so it can't be used again
myList.RemoveAt(randomNumber)
'Reduce count by 1
count -= 1
Loop
Else
MessageBox.Show("Problem with that number you entered")
End If
can anyone tell me what i'm doing wrong
-
Jun 12th, 2011, 08:14 AM
#2
Frenzied Member
Re: Random from one listbox to another
Code:
'Mylist gets the values of the existing array
myList.AddRange(Str)
1) you have commented that Mylist gets the value from the array, but you have declared Str as an single variable
Code:
value cannot be null. parameter name: collection
2) since Str is only but declared but not initialized you are getting the error
Last edited by aashish_9601; Jun 12th, 2011 at 08:18 AM.
-
Jun 12th, 2011, 08:40 AM
#3
Thread Starter
Lively Member
Re: Random from one listbox to another
-
Jun 12th, 2011, 09:36 AM
#4
Re: Random from one listbox to another
Try this:
vb Code:
Dim Str() As String = {"Item1", "Item2", "Item3", "Item4", "Item5"}
Dim myList As New List(Of String)
Dim rnd As New System.Random
Dim randomNumber As Integer = 0
Dim count As Integer = 0
myList.AddRange(Str)
Me.lbxRandom.Items.Clear()
If Integer.TryParse(tbxRandom.Text, count) Then
Do While count <> 0
randomNumber = rnd.Next(0, myList.Count)
Me.lbxRandom.Items.Add(myList(randomNumber))
myList.RemoveAt(randomNumber)
count -= 1
Loop
Else
MessageBox.Show("Problem with that number you entered")
End If
Tom
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jun 12th, 2011, 10:51 AM
#5
Thread Starter
Lively Member
Re: Random from one listbox to another
That works great if creating a new 1 dimension array using items 1-5, but how do I create an array from a list of strings in the lbxdata listbox? I want to randomly choose a number (tbxrandom) from the lbxdata listbox and put that random list in the lbxRandom listbox. I tried changing to
Dim Str() as string=lbxdata.items.tostring but get a an error saying "Value of type string cannot be converted to 1-dimensional array of string"
-
Jun 12th, 2011, 11:22 AM
#6
Re: Random from one listbox to another
Ok - I was trying to come up with a solution to post 2 and didn't read your initial post well enough apparently 
Could this be what you're after:
vb Code:
Dim rnd As New System.Random
Dim randomNumber As Integer = 0
Dim count As Integer = 0
Me.lbxRandom.Items.Clear()
If Integer.TryParse(tbxRandom.Text, count) AndAlso count <= lbxData.Items.Count Then
Do While count <> 0
randomNumber = rnd.Next(0, lbxData.Items.Count)
Me.lbxRandom.Items.Add(lbxData.Items(randomNumber))
'Uncomment this line if you want the items deleted in lbxData
'lbxData.Items.RemoveAt(randomNumber)
count -= 1
Loop
Else
MessageBox.Show("Problem with that number you entered")
End If
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jun 12th, 2011, 11:47 AM
#7
Thread Starter
Lively Member
Re: Random from one listbox to another
That works great, thank you so much for the help.
-
Jun 12th, 2011, 12:02 PM
#8
Re: [RESOLVED] Random from one listbox to another
Code:
Private Sub Form1_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
lbxData.Items.AddRange(New String() {"car", "boat", "motorcycle", "truck", "plane", "submarine"})
End Sub
Dim PRNG As New System.Random 'note - declared in namespace
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
lbxRandom.Items.Clear()
Dim count As Integer
'textbox for user to choose the number of randomly generated items
If Integer.TryParse(tbxRandom.Text, count) Then
For x As Integer = 1 To count
lbxRandom.Items.Add(lbxData.Items(PRNG.Next(lbxData.Items.Count)))
Next
Else
MessageBox.Show("Problem with that number you entered")
End If
End Sub
Don't make this complicated. Also, the random should be declared in the namespace, not in the button.
-
Jun 12th, 2011, 04:35 PM
#9
Thread Starter
Lively Member
Re: [RESOLVED] Random from one listbox to another
I know I resolved this because it appeared to be working. Which it does but I didn't notice that it removed items from the first listbox. with the line uncommented "'lbxData.Items.RemoveAt(randomNumber)" with it commented gives me repeats of the randomly selected items, which I don't want. I'm thinking i'm going to have to create an array from the first listbox then randomly select items from the array. Is this correct? If so how do you accomplish this???
-
Jun 12th, 2011, 04:48 PM
#10
Re: [RESOLVED] Random from one listbox to another
 Originally Posted by irishlad
I know I resolved this because it appeared to be working. Which it does but I didn't notice that it removed items from the first listbox. with the line uncommented "'lbxData.Items.RemoveAt(randomNumber)" with it commented gives me repeats of the randomly selected items, which I don't want. I'm thinking i'm going to have to create an array from the first listbox then randomly select items from the array. Is this correct? If so how do you accomplish this???
Let me see if I got this right - you wan't to add random items from one listbox to another. You wan't the items to not be removed from the first listbox on a runthrough, but yet they cannot be randomly added in any succeeding run? And you wan't the 2nd listbox to be cleared from all items on each run?
So the first listbox is sort of a deck of cards and the run deals a hand (of user-defined size) to the 2nd listbox. On any following run the cards on the hand are discarded and a new hand is dealt from the limited number of cards left.
Am I right in my assumptions?
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jun 12th, 2011, 05:15 PM
#11
Thread Starter
Lively Member
Re: [RESOLVED] Random from one listbox to another
lol sort of. I work in a prison. The user will populate the first listbox with cells for their particular unit (beds can be added and deleted at any time) and is saved to a text file. When the user loads the program the first listbox will load with the cells that have been added (but I don't want this to be edited unless they so choose to, ie removed from the listbox with the "'lbxData.Items.RemoveAt(randomNumber)" because if they save again they will loose the info). There can be upward of 1200 cells. They will randomly choose, say 100, cells to be selected for searches or urinaylisis testing. I want that list populated in the second listbox, but not remove any of the items in the first listbox. Hope this makes it more clear
-
Jun 12th, 2011, 05:25 PM
#12
Re: [RESOLVED] Random from one listbox to another
 Originally Posted by irishlad
lol sort of. I work in a prison. The user will populate the first listbox with cells for their particular unit (beds can be added and deleted at any time) and is saved to a text file. When the user loads the program the first listbox will load with the cells that have been added (but I don't want this to be edited unless they so choose to, ie removed from the listbox with the "'lbxData.Items.RemoveAt(randomNumber)" because if they save again they will loose the info). There can be upward of 1200 cells. They will randomly choose, say 100, cells to be selected for searches or urinaylisis testing. I want that list populated in the second listbox, but not remove any of the items in the first listbox. Hope this makes it more clear
Ok - think I have a clearer understanding of the problem now. Cells will never be removed by the random picks to testing. Cells can be added or removed by other sources not discussed here. Would it be sensible to add some sort of tag ('last checked on date' or similar) to the currently selected cells for analysis, and not allow any cell that has been checked within a specified timeframe to be selected for analysis?
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Jun 13th, 2011, 07:31 PM
#13
Re: [RESOLVED] Random from one listbox to another
Code:
Dim PRNG As New System.Random 'note - declared in namespace
Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
lbxRandom.Items.Clear()
Dim count As Integer
Dim tl As New List(Of Integer)
'textbox for user to choose the number of randomly generated items
If Integer.TryParse(tbxRandom.Text, count) Then
For x As Integer = 1 To count
Dim idx As Integer
If lbxRandom.Items.Count = lbxData.Items.Count Then
'error
Stop
End If
Do
idx = PRNG.Next(lbxData.Items.Count)
Loop While tl.Contains(idx)
tl.Add(idx)
lbxRandom.Items.Add(lbxData.Items(idx))
Next
Else
MessageBox.Show("Problem with that number you entered")
End If
End Sub
-
Jun 15th, 2011, 06:42 AM
#14
Thread Starter
Lively Member
Re: [RESOLVED] Random from one listbox to another
TJ thanks for all the help. No I think that is a bit overkill as this list will only be generated once a month and won't matter who was randomly chosen in the past. dbasnett's code works great too, only it still removes the selections from the lbxdata listbox. If I can get suggestions to tweak his/her code to send the lbxdata to an array and then randomly choose from the array where it won't matter if the items are removed after selection and placed in the lbxRandom listbox, I think is what i'm looking for.
-
Jun 15th, 2011, 06:45 AM
#15
Thread Starter
Lively Member
Re: [RESOLVED] Random from one listbox to another
Arrrggg. Never mind, forgot to do something. dbasnett's code is exactly was I was looking for. Thanks again for the help guys!!!!!!
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
|