How do I access multiple textboxes using For Loop

Get help using Construct 2

Post » Mon Mar 19, 2018 3:42 pm

Hi,

I'm used to programming in different language and I'm giving construct a try to see if I can make prototypes quicker using it.

I'm struggle a little bit with what should be basic stuff, but I can't figure out if theres a quick way of doing this.

I need to pull multiple values out of an array and enter each one into a series of text boxes.

Normally I would right something similar to this:

Code: Select all
for (i = 0; i < 10; i ++){
   this.myTextBox[i] = myArray[i,0]
}


However, when using C2 I can't see a way to access multiple text boxes at once using a for loop. Do I have to write to each one separately?

Code: Select all
myTextBox0: Set text to myArray.At(0,0)
myTextBox1: Set text to myArray.At(1,0)
myTextBox2: Set text to myArray.At(2,0)
myTextBox3: Set text to myArray.At(3,0)
myTextBox4: Set text to myArray.At(4,0)
myTextBox5: Set text to myArray.At(5,0)
myTextBox6: Set text to myArray.At(6,0)
myTextBox7: Set text to myArray.At(7,0)
myTextBox8: Set text to myArray.At(8,0)
myTextBox9: Set text to myArray.At(9,0)


Surely I'm missing something obvious here??
B
5
Posts: 13
Reputation: 251

Post » Mon Mar 19, 2018 5:45 pm

You are using seperate text box objects?

One way is to use a single text box object, with multiple instances.

Then you can refer to each one by IID, using loopindex as your IID. (Pick Nth instance system condition)
Mistakes were made.
B
60
S
30
G
132
Posts: 1,931
Reputation: 74,339

Post » Mon Mar 19, 2018 9:26 pm

Yes I'm using separate text box objects as if I just use one then they all show them same text each time.
Also I'm not sure if it makes a difference but its the text field object I'm using not a text box (my mistake for use of phrase)
B
5
Posts: 13
Reputation: 251

Post » Mon Mar 19, 2018 9:34 pm

Text... field? There are Text box objects and Text objects.... unless I'm totally missing something.

If you use multiple instances of the same text object, you have to "pick" to filter out which text object you want to manipulate by using conditions. Use the Pick Nth instance condition, and any actions will only be applied to that particular instance.
Mistakes were made.
B
60
S
30
G
132
Posts: 1,931
Reputation: 74,339

Post » Tue Mar 20, 2018 1:55 am

oosyrag is right, you need to use instances, not separate objects.

Create one Text object, add several instances of this object on the layout. (just copy/paste)
If you arrange them in the order they were created you can do this:

System For each Text -> Set text to myArray.At(loopindex,0)

Or you can add an instance variable "ID" to this text object and pick instances by ID:

for "i" = 0 to 9
....Text compare variable ID=loopindex -> Set text to myArray.At(loopindex,0)
B
17
S
9
G
156
Posts: 2,202
Reputation: 82,985

Post » Wed Mar 21, 2018 1:06 pm

Thanks guys. I had actually figured it out in the end.

Am using the following with several instances of the same object

Code: Select all
System -> For "i" from 0 to 4
    System -> Pick myTextBox0 instance loopindex("i"):   myTextBox0 -> Set text to myArray.At(loopindex("i"), 0)


Still finding my feet with C2, its difficult when you're used to hand coding stuff to then work with event and behaviors
B
5
Posts: 13
Reputation: 251

Post » Thu Mar 29, 2018 5:38 pm

now I have this working I'm having a similar related issue that I can't figure out.

I have a series of text boxes that the user enters numbers into. On a button press these are compared to data in an array.
If the input and array entry match a text field is marked "true"

Image

This works fine for 1 text box and 1 text field, but how do I do it if these is multiple of each?

How can you Pick nth instance and compare at the same time??
B
5
Posts: 13
Reputation: 251

Post » Thu Mar 29, 2018 8:22 pm

Idea is similar, you can still pick nth instance by loopindex, just add a Pick All condition before Pick Nth instance with loopindex. Then it will go through each instance of your textbox one at a time.
Mistakes were made.
B
60
S
30
G
132
Posts: 1,931
Reputation: 74,339

Post » Fri Mar 30, 2018 10:18 am

x3fold if you are new to Construct 2, use my tutorials you will find lots of things to learn from it, also get my DEMOS from DROPBOX
B
267
S
51
G
16
Posts: 2,039
Reputation: 43,934

Post » Fri Mar 30, 2018 11:16 am

still not able to get this to work

I need it so that when the button is pressed the input into ansBox0 is checked against the the entry in an array, and if it is correct the it changes true/false0 to "true"

Image

I can do it one ansBox, true/false field at a time, but I need to be able to do it for all at the same time - its possible in every other programming language using a For loop, but I cant figure out how in C2.

My current code is:
Image

but this changes all true/false fields to "true" if only one of the ansBox entry fields matches the array
B
5
Posts: 13
Reputation: 251

Next

Return to How do I....?

Who is online

Users browsing this forum: burak22, kelany, The Mnk and 2 guests