1

I'm working on an assignment that takes in 20 answers from user using 20 textboxes, grade the answers and output the correct answer choices to 20 label boxes. The input answers and the answer keys are store in array. What's the best method to pass values from the answer key array to all appropriate labels in the form? Right now I'm using this method:

For i As Integer = 21 To intLblNum
    For intCount = 0 To (strAnswers.Length() - 1)
        gradeResult.Controls("Label" & i.ToString).Text = strAnswers(intCount)
    Next
Next

But as the result, all of my label boxes contain only the last element from the answer key array. What'd I do wrong?

1
  • What is the value of intLblNum the moment the first loop is entered? Commented Jun 26, 2013 at 5:11

2 Answers 2

2

One problem that I see is that you loop through all 20 answers for each label, you need to have one loop and offset the value to account for your Label names. Something like this should work for you.

Dim maxEntrys As Integer = 19

For i = 0 To maxEntrys
    gradeResult.Controls("Label" & (21 + i)).Text = strAnswers(i)
Next
Sign up to request clarification or add additional context in comments.

2 Comments

works perfectly! thank you so much. As for passing values from textbox to array, the method is similar?
@kevinnguyen Yes it would be done the same way, glad I was able to help.
1

Assuming you are in WinForms, each controls has a Tag property of type object that can be used to store custom data. It is not considered good design to use control naming as a means of associating data with controls.

Consider using a class to hold your question/answer data, make an array or preferably collection of them and set each control's Tag property to the appropriate instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.