Using arrays of controls or custom controls provides a very powerful means of dealing with certain kinds of data manipulation. In this example we will be using this technique to make a simple quiz program.
Step 1 – Creating a Custom Control
Add a new Class to your project. Call it “answerField” and enter TextField as its Super. To answerField add two properties: result as boolean and rightAnswer as string.
Now we will add some code to answerField. Put the following code in the TextChange event of answerField. We will see a bit further on how this code utilizes the two properties (result as boolean, and rightAnswer as string) to keep track of right and wrong answers.
Step 2 – Creating the Control Array
To Window1 add a Label control for the first question. Also add an TextField for the answer. In the Inspector set the super of the TextField to “answerField” and name it “responseField”.
Repeat this step to add another Label and TextField. Make sure the new TextField’s super is “answerField”. Again name the TextField “responseField” and hit Enter. When you name the second answerField the same as the first this dialog will appear. Click OK.
This creates a control set.
Keep adding questions and responseFields in this way until you have 5 questions and responseFields.