Results 1 to 15 of 15

Thread: [RESOLVED] Random from one listbox to another

  1. #1

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Resolved [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

  2. #2
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    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

  3. #3

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: Random from one listbox to another

    So how do I do that?

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Random from one listbox to another

    Try this:
    vb Code:
    1. Dim Str() As String = {"Item1", "Item2", "Item3", "Item4", "Item5"}
    2.         Dim myList As New List(Of String)
    3.         Dim rnd As New System.Random
    4.         Dim randomNumber As Integer = 0
    5.         Dim count As Integer = 0
    6.  
    7.         myList.AddRange(Str)
    8.  
    9.         Me.lbxRandom.Items.Clear()
    10.  
    11.         If Integer.TryParse(tbxRandom.Text, count) Then
    12.  
    13.             Do While count <> 0
    14.  
    15.                 randomNumber = rnd.Next(0, myList.Count)
    16.                 Me.lbxRandom.Items.Add(myList(randomNumber))
    17.                 myList.RemoveAt(randomNumber)
    18.                 count -= 1
    19.  
    20.             Loop
    21.  
    22.         Else
    23.             MessageBox.Show("Problem with that number you entered")
    24.         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)

  5. #5

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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"

  6. #6
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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:
    1. Dim rnd As New System.Random
    2.         Dim randomNumber As Integer = 0
    3.         Dim count As Integer = 0
    4.  
    5.         Me.lbxRandom.Items.Clear()
    6.  
    7.         If Integer.TryParse(tbxRandom.Text, count) AndAlso count <= lbxData.Items.Count Then
    8.  
    9.             Do While count <> 0
    10.  
    11.                 randomNumber = rnd.Next(0, lbxData.Items.Count)
    12.                 Me.lbxRandom.Items.Add(lbxData.Items(randomNumber))
    13.                 'Uncomment this line if you want the items deleted in lbxData
    14.                 'lbxData.Items.RemoveAt(randomNumber)
    15.                 count -= 1
    16.  
    17.             Loop
    18.  
    19.         Else
    20.             MessageBox.Show("Problem with that number you entered")
    21.         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)

  7. #7

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    Re: Random from one listbox to another

    That works great, thank you so much for the help.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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???

  10. #10
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: [RESOLVED] Random from one listbox to another

    Quote Originally Posted by irishlad View Post
    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)

  11. #11

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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

  12. #12
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: [RESOLVED] Random from one listbox to another

    Quote Originally Posted by irishlad View Post
    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)

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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.

  15. #15

    Thread Starter
    Lively Member irishlad's Avatar
    Join Date
    Oct 2006
    Location
    Arizona
    Posts
    108

    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
  •  



Click Here to Expand Forum to Full Width