Results 1 to 4 of 4

Thread: How to have an array of labels randomly appear

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    14

    Question How to have an array of labels randomly appear

    I have 20 labels that belong to an array. Then i have another Label that i want them to copy to in a random order. I'm doing a test for children and they have 10 questions that they have to answer, but i want these questions to be randomly selected out of the 20 labels. At the moment my code is:
    Private Sub cmdcontinue_Click()
    Dim quest As String, poss As String, Index(1 To 20) As Integer
    Randomize
    poss = (RndIndex * 10) + 1
    quest = lblquestion.Caption
    poss = lblrand.count
    quest = poss
    End Sub

    At the moment it isn't doing anything, and before it was only showing the number 20????

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: How to have an array of labels randomly appear

    Code:
    Private Sub cmdcontinue_Click()
    Dim quest As String, poss As String, Index(1 To 20) As Integer
    Randomize
    poss = (RndIndex * 10) + 1
    quest = lblquestion.Caption
    poss = lblrand.count
    quest = poss
    End Sub
    Have few questions.

    1.The variable Poss is declared String, but assigend an Integer?
    2. RndIndex - where is it declared?
    3. what is the purpose of this line quest = poss?
    4. you have poss = (RndIndex * 10) + 1 and poss = lblrand.count. What is the purpose?
    5. how do you want to select the lables? I mean , do you randomly set their visible=True, or something else?

    On using Random Numbers, tkae a look at this

    http://www.vbforums.com/showthread.php?t=281172


    IIF(Post.Rate > 0 , , )

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: How to have an array of labels randomly appear

    Maintain the questions and answers in a string array rather than using form controls to hold values. In the string array, each array element will have a question mark as delimiter between question and answer.

    Shuffle the string array, or randomize the order of the array elements. Do a search, this has been demonstrated several times.

    Iterate through the first 10 elements of the shuffled array (out of possible 20), you will have your 10 random questions and answers, non-repeating. Use Split() function to separate question from answer, eg.
    Code:
    For i = 1 to 10 'or 0 to 9 depending on the indices your using
       label1(i).Caption = Split(strArr, "?")(0) 'question stored in Caption property
       label1(i).Tag = Split(strArr, "?")(1)  'answer stored in Tag property
    Next

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    14

    Re: How to have an array of labels randomly appear

    Right, thanks

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