Results 1 to 7 of 7

Thread: Selecting a Random items from a listbox and removing it.

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Selecting a Random items from a listbox and removing it.

    What do I add to this when that random selection goes into lstBlue. I want it removed after it was added to lstBlue.

    I got the selecting random to work so far.
    Code:
            Dim selectRnd As New Random
            lstBlue.Items.Add(lstAll.GetItemText(lstAll.Items(selectRnd.Next(0, lstAll.Items.Count))))

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

    Re: Selecting a Random items from a listbox and removing it.

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

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Selecting a Random items from a listbox and removing it.

    You'll need to separate the random number selection to assign it it to a variable which you can use in both the add to lstBlue and remove from lstBlue commands.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Selecting a Random items from a listbox and removing it.

    Quote Originally Posted by dbasnett View Post
    What?
    I would like it after it selects that random string. It will them remove itself from the listbox.

  5. #5

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Selecting a Random items from a listbox and removing it.

    Could you give me a example. All I need it to do is when the random string is selected from the listbox it will appear the lstBlue and then remove itself from the original listbox it was taken from so I don't get repeats.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Selecting a Random items from a listbox and removing it.

    As has been said, don't put all your code into that one line. Generate a random number and assign it to a variable. That's one line. You can then use the number multiple times if you want. You can use it to index the Items collection of the first ListBox to get that item, which you can then add to the second ListBox. Finally, you have the original item and its index, so you can use Remove or RemoveAt to remove it from the first ListBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Re: Selecting a Random items from a listbox and removing it.

    Quote Originally Posted by jmcilhinney View Post
    As has been said, don't put all your code into that one line. Generate a random number and assign it to a variable. That's one line. You can then use the number multiple times if you want. You can use it to index the Items collection of the first ListBox to get that item, which you can then add to the second ListBox. Finally, you have the original item and its index, so you can use Remove or RemoveAt to remove it from the first ListBox.
    Thank you for your help! I will test this out

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