13 Replies Latest reply on Feb 9, 2009 4:13 PM by PeakDigital

    Need help with dynamic app controls

    PeakDigital Level 1
      I am new to Flex and am trying to apply some of the concepts I have done before in other languages (VB6, PHP) to Flex. My major project right now involves building apps based on lists of controls defined in a database.

      I have a simple custom component for a text input consisting of a label and textinput controls (This is my first attempt to attach code to a post on this forum, I hope it works)

      The creation of the controls is fine, but I am running into dead ends trying to read back the controls that were created and their values. I have been experimenting with testVBox.getChildAt(i) in a for loop, but it isn't working.

      Can someone point me in the right direction on this?

      Thank you.
      Paul
        • 1. Re: Need help with dynamic app controls
          Gregory Lafrance Level 6
          Not clear on what is not working. After creating the controls are you trying to write them back to the database and having difficulties? What difficulties are you having, like what errors are you getting?
          • 2. Need help with dynamic app controls
            PeakDigital Level 1
            Yes, that is correct. The controls are being created and populated as I want, but I cannot figure out how to grab all the controls and their values so I can validate and send back to the database.

            This is the only error I am getting:
            1119: Access of possibly undefined property strLabelText through a reference with static type flash.display:DisplayObject.

            That shows in the IDE for the attached function on the line "strFieldDump = strFieldDump + currControl.strLabelText;"

            The function is attached to a button's click event, but no errors occur when I click the button - nothing happens.
            • 3. Re: Need help with dynamic app controls
              Gregory Lafrance Level 6
              The controls are probably being returned from getChildAt as Objects, and need to be cast to their real types.
              • 4. Re: Need help with dynamic app controls
                PeakDigital Level 1
                I discovered the Form includes a label along with whatever input is needed and also handles alignment, so I have switched my method to using FormItems intead of custom components.

                I am still stuck though. After more digging I was able to understand your advice about casting, and was able to at least get the real ID of the FormItem control by using this syntax:

                var currFormItem:FormItem = FormItem(formTest.getChildAt(i));
                strFieldDump = strFieldDump + "\r control name: " + currFormItem.id;

                Unfortunately, I still can't access the actual TextInput or DateInput (or other input control) within that FormItem. I have tried running a debug and can't see anything in the variables pane that show the children.

                Am I missing something really obvious? Can anyone point me to a good tutorial or explanation of how to access child objects?

                Thanks.
                Paul

                • 5. Need help with dynamic app controls
                  PeakDigital Level 1
                  <deleted duplicate post - server connection problem>
                  • 6. Need help with dynamic app controls
                    PeakDigital Level 1
                    <removed duplicate post>
                    • 7. Re: Need help with dynamic app controls
                      Level 7

                      "PeakD" <webforumsuser@macromedia.com> wrote in message
                      news:gmfgt7$dvf$1@forums.macromedia.com...
                      >I discovered the Form includes a label along with whatever input is needed
                      >and
                      > also handles alignment, so I have switched my method to using FormItems
                      > intead
                      > of custom components.
                      >
                      > I am still stuck though. After more digging I was able to understand your
                      > advice about casting, and was able to at least get the real ID of the
                      > FormItem
                      > control by using this syntax:
                      >
                      > var currFormItem:FormItem = FormItem(formTest.getChildAt(i));
                      > strFieldDump = strFieldDump + "\r control name: " + currFormItem.id;
                      >
                      > Unfortunately, I still can't access the actual TextInput or DateInput (or
                      > other input control) within that FormItem. I have tried running a debug
                      > and
                      > can't see anything in the variables pane that show the children.
                      >
                      > Am I missing something really obvious? Can anyone point me to a good
                      > tutorial
                      > or explanation of how to access child objects?

                      You can store references to the children in an array yourself, or you can
                      use Repeater and let it build the array for you.

                      HTH;

                      Amy


                      • 8. Re: Need help with dynamic app controls
                        PeakDigital Level 1
                        Amy,

                        Thank you for the suggestion. It would make sense to store the references in an array, since the array already exists from building the controls.

                        I'm still having a mental block on understanding how to locate and reference controls created at runtime though. My array stores the ID of the control in a node titled "DBFieldName". The controls could be any of the standard input controls - text, datefield, textarea, checkbox, combobox, etc.

                        The only method I have seen in my reading so far that appears to let me grab a control by ID/name is getChildByName(), but it seems that unless I built a lot of code to first determine what kind of control I am looking at, I can't even try to get the text property of the control.

                        I tried the following code:
                        strTestGetControlsFromArray += "; value is " + this.getChildByName(currControlFromArray.DBFieldName).text

                        and the IDE problems panel gives the error "1119: Access of possibly undefined property text through a reference with static type flash.display:DisplayObject." .... so it appears I am right back to the problem of having to figure out the control type, cast it, and then ask for the current value of the control.

                        Is there a more generic, concise way to access all controls in an app? I'm feeling that for all the reading and research I have done, I must be overlooking something.

                        Thanks for your help.
                        Paul
                        • 9. Need help with dynamic app controls
                          PeakDigital Level 1
                          I have been continuing to research and experiment. Here is some code I came up with that seems to be on the right track, hopefully others can benefit from it. This is for a Form object, to find out what FormItems are in it and what their values are.

                          If anyone has suggestions to improve its efficiency, I'd like to see them.
                          • 10. Re: Need help with dynamic app controls
                            rtalton Level 4
                            Are you just trying to get each control's value? Or something else?
                            • 11. Re: Need help with dynamic app controls
                              rtalton Level 4
                              If you are trying to get the user-entered values of various controls, your idea will work, however, you might try something like this:
                              • 12. Re: Need help with dynamic app controls
                                Level 7

                                "PeakD" <webforumsuser@macromedia.com> wrote in message
                                news:gmhte0$dpl$1@forums.macromedia.com...
                                > Amy,
                                >
                                > Thank you for the suggestion. It would make sense to store the references
                                > in
                                > an array, since the array already exists from building the controls.
                                >
                                > I'm still having a mental block on understanding how to locate and
                                > reference
                                > controls created at runtime though. My array stores the ID of the control
                                > in a
                                > node titled "DBFieldName". The controls could be any of the standard input
                                > controls - text, datefield, textarea, checkbox, combobox, etc.

                                And ID created at runtime is worthless. Store a reference to the actual
                                child. You have a reference to the child already:

                                addChild(childComponent)

                                Just add the child that you already have a reference to into your array:

                                childArrray.push(childComponent)

                                Presumably since you had some method to how you created the children, once
                                you know which child is at which index, you won't have to do all that type
                                checking.

                                Hope this clarifies;

                                Amy


                                • 13. Re: Need help with dynamic app controls
                                  PeakDigital Level 1
                                  rtalton, Thank you for the sample code. There are some new items in there I haven't seen yet, I'll do some experimenting.

                                  ----------------
                                  Amy, Thank you for the input. I have a lot to learn about how Flex can manage data and objects. This will give me one more thing to work with.