0 Favourites

How do I access multiple textboxes using For Loop

  • 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:

    for (i = 0; i < 10; i ++){
    	this.myTextBox[i] = myArray[i,0]
    }[/code:1lwa1qi6]
    
    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:1lwa1qi6]
    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)
    [/code:1lwa1qi6]
    
    Surely I'm missing something obvious here??
  • 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)

  • 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)

  • 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.

  • 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)

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

    Am using the following with several instances of the same object

    System -> For "i" from 0 to 4
        System -> Pick myTextBox0 instance loopindex("i"):   myTextBox0 -> Set text to myArray.At(loopindex("i"), 0)
    [/code:1yheo6zg]
    
    Still finding my feet with C2, its difficult when you're used to hand coding stuff to then work with event and behaviors
  • 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"

    https://ibb.co/fGOFkS

    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??

  • 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.

  • 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

  • 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"

    https://imgbb.com/

    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:

    https://ibb.co/kEGszn

    but this changes all true/false fields to "true" if only one of the ansBox entry fields matches the array

  • I think you need to pick a 'trueFalse' Text instance also. I think that's why everything is set to True.

  • I think you need to pick a 'trueFalse' Text instance also. I think that's why everything is set to True.

    But how do you do that when you're picking actions?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • In the same Condition in which you pick ansBox, you can put another Condition which selects the 'trueFalse' Text instance. Put something like:

    Pick trueFalse instance loopindex("i")[/code:mebxa4zi]
    
    That's assuming that `loopindex("i")` corresponds with the 'trueFalse' Text instance number.
  • Ah, I see!

    I didn't realize that putting in the condition would then let if affect the action.

    Works perfectly, thanks!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
99 10,155
karshinkoff's avatar
karshinkoff
Unread hot topic
56 4,736
MPPlantOfficial's avatar
MPPlantOfficial
Unread hot topic
76 9,273
Pandadoor's avatar
Pandadoor