Results 1 to 4 of 4

Thread: [2008] Randomly check items in a CheckedListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    [2008] Randomly check items in a CheckedListBox

    Hi, I'm new to VB and I have a few problems.
    Ok so what I want is to select random items from a CheckedListBox and then print them to a sheet of paper. I also want to define how many random items are checked via a TextBox. Let's say I have 50 items in the CheckedListBox and I want to randomly select only 10 (more or less - user defined via the TextBox) of them and print them to a sheet of paper.

    Thanks for the help in advance. :P

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Randomly check items in a CheckedListBox

    Once the user enters the number in a textbox, convert to int and perform a loop that many times (say, x).

    In each iteration, call the Random class' Next() method, but limit it to the number of items in the checkboxlist.

    So you can have something like

    randomObject.Next(0, CheckBoxList1.Items.Count -1)

    And whatever you get back from there is the index of the item you should check.

    However, to add to this a bit, you will also need to ensure that the number returned hasn't been returned in a previous iteration, so you may want to start by generating the 10 random numbers first and storing them in a list.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    2

    Re: [2008] Randomly check items in a CheckedListBox

    I'm a little embarrassed to say it but... I really am new to VB and I don't have almost any idea how to start "codding". If you are not too busy could you uhm... be able to write the code for me, with the print function I'd be really grateful.

    P.S: I'm so embarrassed right now. :S

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Randomly check items in a CheckedListBox

    There's no need to be embarrassed. I won't write the code for you because you're a beginner and I feel that you should learn this stuff.

    Take it step by step. First, read the value from the textbox.
    Then, set up a for-next loop. Try this out first and if you get stuck, post again asking about it.

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