13 Replies Latest reply on Sep 12, 2006 10:35 AM by Matlus

    Finding a control by id?

    Matlus Level 1
      Is there a way to get a reference of a control given its id in the form of a string?

      Something like:
      var obj:Sprite = Application.findControl("btnOk");
        • 1. Re: Finding a control by id?
          Matlus Level 1
          I found a getChildByName() method. I'll try that. If is is what I think it is, I would have thought it would be called getChildById().
          • 2. Re: Finding a control by id?
            Matlus Level 1
            Ok, the getChildByName really gets children by id (note that DisplayObjects don't seem to have an intrinsic id property but do have a name property that matches the value of the id attribute set decleratively.).

            Nonetheless, in order to truely "findControl" one will have to recursively iterate over all children's children attempting to find the one control. This will be a major performance drawback unless one could assemble a hash table or dictionary of sorts by iterating over everything once.

            • 3. Re: Finding a control by id?
              inlineblue Level 1
              You can't really do what you describe because each component has its own namespace and, therefore, control names are not necessarily unique when components are nested (as is often the case). You'll have to roll your own solution that takes care of name conflicts.
              • 4. Re: Finding a control by id?
                Matlus Level 1
                Can you explain that please? From my little experience with Flex I am forced to have a unique "id" for every control in my application.

                • 5. Re: Finding a control by id?
                  inlineblue Level 1
                  It sounds like you've only created apps using the main Application MXML file. In such a case, there is only one namespace and getting a control by a string id is trivial: this["my_component_id"].

                  However, for any moderately complicated application, the more common scenario is that the application is broken up into multiple components, each in its own MXML file. These components are then used in the main application MXML file to build up the app. Each component has its own namespace, meaning that each component can have a button named "btnOk". If you attempted to perform a recursive search from the top Applcation object, you would run into name conflicts.
                  • 6. Re: Finding a control by id?
                    Matlus Level 1
                    Well, I've yet created my first app :).

                    I have yet to wrap my head around what an "application" really is in Flex too. So are you saying an application may be comprised of many applications? If so, it seems to like Flex has some issues with terminology :).

                    Thanks for your input nonetheless. I appreciate the insight.

                    • 7. Re: Finding a control by id?
                      inlineblue Level 1
                      No, I'm saying that an application is potentially composed of many components. It's just that the main application and the components are defined using MXML files. In any application, there is only one main MXML file that contains the <mx:Application> tag. The rest are components. Read through the docs about creating custom components for a better idea.
                      • 8. Re: Finding a control by id?
                        ntsiii Level 3
                        You may be overcomplicating this. If a control has an id, you can reference it directly:
                        <mx:Label id="lab1" />

                        In as:
                        lab1.text = "Some Text"; //Some Text will display in the label.

                        If I have misunderstood, never mind.

                        Only put an id on controls you need to reference in code. There is some cost involved with maintaining ids

                        • 9. Re: Finding a control by id?
                          ntsiii Level 3
                          Or as in your first post:
                          import mx.controls.Button;
                          var obj:Button= myButtonId;

                          • 10. Re: Finding a control by id?
                            ntsiii Level 3
                            One more thing.

                            You can use bracket notation to return a referenct to a control who's id is in a variable or is dynamically created.
                            var myIdvariable:String = "myButtonId";
                            var obj:Button= this[myIdvariable];

                            • 11. Re: Finding a control by id?
                              Matlus Level 1

                              I've solved the problem by using a recursive loop. but since you've taken the time to try and help...

                              My issue really was not knowing the ids of controls on a form in the first place so using syntax like:


                              has little meaning unless I can actually get the id in the first place :).

                              I needed to simulate a "form" post (http style). This has now been achived by:
                              1. recursively iterating over all controls in the application.
                              2. While in the loop, depending on the "type" of control the "value" is extracted (using a factory for specialized objects that know how to "extract" the values of the various controls).
                              3. Building a dynamic object that has properties that match the ids of controls found and whose value is set to be the value of the control.
                              4. This object is then passed in as the parameter to the HttpService object's send() method (since this method currently accepts only dynamic objects and not typed objects nor a url encoded string).

                              Q.E.D. (eventually)

                              • 12. Re: Finding a control by id?
                                Kristian Wright Level 1

                                I seem to be having the same issue that you've solved - would you (or anyone) be able to post some example code for me to follow?

                                • 13. Re: Finding a control by id?
                                  Matlus Level 1
                                  What part do you need example code for?