[flexcoders] Re: getChildByName using it to get the value of a textbox
I had to build a dynamic form from a database not so long ago. There is a whole lot more to it but I have boiled it down to the basic functionality. The form component itself (MyForm.as) connects to the database and grabs all the data to build the form dynamically. I have not included any of that code. This is how I did it: main.mxml: ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=absolute xmlns:custom=CustomComponents.* custom:MyForm/ /mx:Application CustomComponents.MyForm.as: package CustomComponents { import mx.containers.Form; import mx.containers.FormItem; import mx.controls.Alert; import flash.events.MouseEvent; import mx.controls.Button; import mx.controls.TextInput; import mx.controls.CheckBox; import mx.controls.TextArea; public class MyForm extends Form { public function MyForm() { super(); var theFormItem:FormItem = new FormItem(); var theTextInputItem:TextInput = new TextInput(); theFormItem.setStyle(fontWeight, bold); theTextInputItem.setStyle(fontWeight, normal); theTextInputItem.id = tester; theTextInputItem.width = 300; theFormItem.label = tester; theFormItem.addChild(theTextInputItem); this.addChild(theFormItem); var submitButton:Button = new Button(); submitButton.label = Submit; submitButton.labelPlacement = center; submitButton.addEventListener(MouseEvent.CLICK, submitButtonClicked); this.addChild(submitButton); } private function submitButtonClicked(event:MouseEvent):void { var theFormFieldsString:String = ; var formChildren:Array = this.getChildren(); for(var i:int = 0 ; i formChildren.length ; i++) { if(formChildren[i] is FormItem) { theFormFieldsString += (formChildren[i] as FormItem).label + ; var formItemChildren:Array = formChildren[i].getChildren(); if(formItemChildren[0] is TextInput) { theFormFieldsString += (formItemChildren[0] as TextInput).text + \n\n; } if(formItemChildren[0] is TextArea) { theFormFieldsString += (formItemChildren[0] as TextArea).text + \n\n; } if(formItemChildren[0] is CheckBox) { theFormFieldsString += (formItemChildren[0] as CheckBox).selected ? Yes : No; theFormFieldsString += \n\n; } } } Alert.show(theFormFieldsString); } } } --- In flexcoders@yahoogroups.com, Jason B [EMAIL PROTECTED] wrote: Yes your code works since its not dynamically building the form but what happens if you try this code...it gets an error like i do ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical horizontalAlign=left creationComplete=loaditem() mx:Script ![CDATA[ import mx.controls.Alert; import mx.controls.TextInput; import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.controls.ComboBox; import mx.controls.Text; import mx.controls.RadioButton; import mx.controls.RadioButtonGroup; import mx.controls.Label; import mx.collections.ArrayCollection; import mx.controls.dataGridClasses.DataGridColumn; import mx.controls.TextArea; import mx.events.*; import mx.validators.*; import mx.managers.ToolTipManager; import mx.managers.IFocusManager; import mx.managers.IFocusManagerComponent; import mx.core.Container; import mx.controls.Alert; import mx.core.ComponentDescriptor; import flash.utils.*; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.net.URLVariables; public function loaditem(){ create_text = new TextInput(); create_text.id = tester; create_text.text =create_text.id; dataBox.addChild(create_text); } private function showText(evt:MouseEvent):void{ var test:TextInput = this.getChildByName(tester) as TextInput; Alert.show(test.text); traceDisplayList(this); } public function traceDisplayList(container:DisplayObjectContainer,
[flexcoders] Re: getChildByName using it to get the value of a textbox
Yes i tried that and while it loops it doesnt give KEY what i need example for (var key:String in dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[key] as TextInput; Alert.show(t.text); //produces error } --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: If you want to loop over the keys (the names of the components), you need a for-in loop: for (var key:String in dynamicallyCreatedComponents) A for-each-in loop loops over the values (the references to the componetns). There is no such thing as a for-each-as loop as you've written below. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Wednesday, August 20, 2008 11:09 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Can i loop through the items in that object example for each(var items:String as dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[items] as TextInput; Alert.show(t.text); //THROWS ERROR } I was hoping to set the array key dynamically and loop through it later? can this be done Gordan --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Tuesday, August 19, 2008 7:38 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname
RE: [flexcoders] Re: getChildByName using it to get the value of a textbox
So put in a trace(key) and see what 'key' is. That might make clear what you're doing wrong. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Thursday, August 21, 2008 5:35 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Yes i tried that and while it loops it doesnt give KEY what i need example for (var key:String in dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[key] as TextInput; Alert.show(t.text); //produces error } --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith [EMAIL PROTECTED] wrote: If you want to loop over the keys (the names of the components), you need a for-in loop: for (var key:String in dynamicallyCreatedComponents) A for-each-in loop loops over the values (the references to the componetns). There is no such thing as a for-each-as loop as you've written below. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Wednesday, August 20, 2008 11:09 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Can i loop through the items in that object example for each(var items:String as dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[items] as TextInput; Alert.show(t.text); //THROWS ERROR } I was hoping to set the array key dynamically and loop through it later? can this be done Gordan --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Tuesday, August 19, 2008 7:38 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a
[flexcoders] Re: getChildByName using it to get the value of a textbox
Can i loop through the items in that object example for each(var items:String as dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[items] as TextInput; Alert.show(t.text); //THROWS ERROR } I was hoping to set the array key dynamically and loop through it later? can this be done Gordan --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Tuesday, August 19, 2008 7:38 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
RE: [flexcoders] Re: getChildByName using it to get the value of a textbox
If you want to loop over the keys (the names of the components), you need a for-in loop: for (var key:String in dynamicallyCreatedComponents) A for-each-in loop loops over the values (the references to the componetns). There is no such thing as a for-each-as loop as you've written below. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Wednesday, August 20, 2008 11:09 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Can i loop through the items in that object example for each(var items:String as dynamicallyCreatedComponents){ var t:TextInput = dynamicallyCreatedComponents[items] as TextInput; Alert.show(t.text); //THROWS ERROR } I was hoping to set the array key dynamically and loop through it later? can this be done Gordan --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith [EMAIL PROTECTED] wrote: Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Tuesday, August 19, 2008 7:38 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
[flexcoders] Re: getChildByName using it to get the value of a textbox
thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
RE: [flexcoders] Re: getChildByName using it to get the value of a textbox
Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Tuesday, August 19, 2008 7:38 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox thanks Tracy could you provide an example of how this is done, examples always help others out, or tell me what i should be searching for to find an example --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt [EMAIL PROTECTED] wrote: A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith gosmith@ wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
[flexcoders] Re: getChildByName using it to get the value of a textbox
is this what you mean i've tried to access but i still get a #1009 error public var dynamicallyCreatedComponents:Object = {}; public function loaditems(){ create_text = new TextInput(); create_text.percentWidth = 100; create_text.id = test; create_text.text =create_text.id; dynamicallyCreatedComponents[test] = create_text; dataBox.addChild(create_text); } public function saveall(){ var t:TextInput = dynamicallyCreatedComponents[test]; try{ Alert.show( dynamicallyCreatedComponents[test].text); }catch(errObject:Error){ Alert.show(errObject.message); } } --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: Declare an instance variable of type Object or Dictionary for storing named references to dynamically created components: public var dynamicallyCreatedComponents:Object = {}; In a method where you create a component at runtime, do var b:Button = new Button(); b.label = OK; dynamicallyCreatedComponents[okButton] = b; Later you can access this button as follows: dynamicallyCreatedComponents[okButton] Gordon Smith Adobe Flex SDK Team
[flexcoders] Re: getChildByName using it to get the value of a textbox
Thanks a lot for the reply... turns out i get an error 1009 when i try to something as simple as an alert var test:TextInput = TextInput(this.getChildByName(inputtext + a)); Alert.show(test.text.toString()); --- In flexcoders@yahoogroups.com, Nik Derewianka [EMAIL PROTECTED] wrote: You need to cast it to the type of object you want to operate on: var test:TextBox = this.getChildByName(inputtext + 1) as TextBox; or var test:TextBox = TextBox(this.getChildByName(inputtext + 1)); The first form will return null if it cannot cast the displayObject to that type as opposed to the second which will cause an error. Of course the Eclipse help system is so utterly useless that searching for 'as' will return nothing and displayObject will not be in the first 3 items you search for. Regards, Nik
Re: [flexcoders] Re: getChildByName using it to get the value of a textbox
That #1009 error means that it returned a null from the first line because it couldn't find the child with that name. getChildByName is not recursive so you need to be mindful of the nesting of your requested object. The following app has the functionality working as you need it, but it also has a really handy listing function that displays all of the current children recursively (outputs to the console window in Flex, if you have the debug player) so that you can see the children that are present and how they are nested. Regards, Nik ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical horizontalAlign=left mx:Script ![CDATA[ import mx.controls.Alert; private function showText(evt:MouseEvent):void{ var test:TextInput = this.getChildByName(test + 1) as TextInput; Alert.show(test.text); traceDisplayList(this); } public function traceDisplayList(container:DisplayObjectContainer, indentString:String = -):void{ var child:DisplayObject; for (var i:uint=0; i container.numChildren; i++) { child = container.getChildAt(i); trace(indentString, child, child.name); if (container.getChildAt(i) is DisplayObjectContainer) { traceDisplayList(DisplayObjectContainer(child), + indentString) } } } ]] /mx:Script mx:TextInput id=test1/ mx:Button label=Button click=showText(event)/ /mx:Application
[flexcoders] Re: getChildByName using it to get the value of a textbox
Yes your code works since its not dynamically building the form but what happens if you try this code...it gets an error like i do ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical horizontalAlign=left creationComplete=loaditem() mx:Script ![CDATA[ import mx.controls.Alert; import mx.controls.TextInput; import mx.managers.PopUpManager; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; import mx.controls.Alert; import mx.controls.ComboBox; import mx.controls.Text; import mx.controls.RadioButton; import mx.controls.RadioButtonGroup; import mx.controls.Label; import mx.collections.ArrayCollection; import mx.controls.dataGridClasses.DataGridColumn; import mx.controls.TextArea; import mx.events.*; import mx.validators.*; import mx.managers.ToolTipManager; import mx.managers.IFocusManager; import mx.managers.IFocusManagerComponent; import mx.core.Container; import mx.controls.Alert; import mx.core.ComponentDescriptor; import flash.utils.*; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.net.URLVariables; public function loaditem(){ create_text = new TextInput(); create_text.id = tester; create_text.text =create_text.id; dataBox.addChild(create_text); } private function showText(evt:MouseEvent):void{ var test:TextInput = this.getChildByName(tester) as TextInput; Alert.show(test.text); traceDisplayList(this); } public function traceDisplayList(container:DisplayObjectContainer, indentString:String = -):void{ var child:DisplayObject; for (var i:uint=0; i container.numChildren; i++) { child = container.getChildAt(i); trace(indentString, child, child.name); if (container.getChildAt(i) is DisplayObjectContainer) { traceDisplayList(DisplayObjectContainer(child), + indentString) } } } ]] /mx:Script mx:VBox id=dataBox/mx:VBox mx:TextInput id=test1/ mx:Button label=Button click=showText(event)/ /mx:Application --- In flexcoders@yahoogroups.com, Nik Derewianka [EMAIL PROTECTED] wrote: That #1009 error means that it returned a null from the first line because it couldn't find the child with that name. getChildByName is not recursive so you need to be mindful of the nesting of your requested object. The following app has the functionality working as you need it, but it also has a really handy listing function that displays all of the current children recursively (outputs to the console window in Flex, if you have the debug player) so that you can see the children that are present and how they are nested. Regards, Nik ?xml version=1.0 encoding=utf-8? mx:Application xmlns:mx=http://www.adobe.com/2006/mxml; layout=vertical horizontalAlign=left mx:Script ![CDATA[ import mx.controls.Alert; private function showText(evt:MouseEvent):void{ var test:TextInput = this.getChildByName(test + 1) as TextInput; Alert.show(test.text); traceDisplayList(this); } public function traceDisplayList(container:DisplayObjectContainer, indentString:String = -):void{ var child:DisplayObject; for (var i:uint=0; i container.numChildren; i++) { child = container.getChildAt(i); trace(indentString, child, child.name); if (container.getChildAt(i) is DisplayObjectContainer) { traceDisplayList(DisplayObjectContainer(child), + indentString) } } } ]] /mx:Script mx:TextInput id=test1/ mx:Button label=Button click=showText(event)/ /mx:Application
[flexcoders] Re: getChildByName using it to get the value of a textbox
Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
RE: [flexcoders] Re: getChildByName using it to get the value of a textbox
A good solution to that issue is to maintian a structure of references pointing to each dynamically created component. I personally like an Associative Array/Object/Hashtable. This is way more positive than attempting to loop through the DOM. There will be plenty examples available. Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Monday, August 18, 2008 5:47 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: getChildByName using it to get the value of a textbox Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith [EMAIL PROTECTED] wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i);
Re: [flexcoders] Re: getChildByName using it to get the value of a textbox
Hi. I wrote a getChildById function for this purpose/use case... I create the grid and its children dynamically at runtime and give the DisplayObjects numerical ids. Its one thing you miss in flex if you're used to using getElementById in javascript. So... this is container specific... I havent gotten round to generifying it yet... private function getChildById(g:Grid, id:int):DisplayObject { for each (var gr:GridRow in g.getChildren()) { for each (var gi:GridItem in gr.getChildren()) { if (!(gi.getChildAt(0) is HBox)) { if (gi.getChildAt(0) is Text) { if (Text(gi.getChildAt(0)).id == String(id)) { return Text(gi.getChildAt(0)); } } if (gi.getChildAt(0) is TextInput) { if (TextInput(gi.getChildAt(0)).id == String(id)) { return TextInput(gi.getChildAt(0)); } } } } } return null; } hth. Kr. Luke. Jason B wrote: Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com, Gordon Smith [EMAIL PROTECTED] wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i); -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links -- Luke Vanderfluit Analyst / Web Programmer e3Learning.com.au 08 8221 6422
RE: [flexcoders] Re: getChildByName using it to get the value of a textbox
This seems very inefficient. I don't see any reason to loop over anything to find a component that you dynamically created. If you know you're going to create a single TextInput, declare an instance var like private var textInput:TextInput; If you know you're going to create a bunch of components that you want to access by index, declare an Array. If you know you're going to create all kinds of components that you want to access by some kind of non-index identifier, declare an Object or a Dictionary that maps the identifier to the reference. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Luke Vanderfluit Sent: Monday, August 18, 2008 3:25 PM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Re: getChildByName using it to get the value of a textbox Hi. I wrote a getChildById function for this purpose/use case... I create the grid and its children dynamically at runtime and give the DisplayObjects numerical ids. Its one thing you miss in flex if you're used to using getElementById in javascript. So... this is container specific... I havent gotten round to generifying it yet... private function getChildById(g:Grid, id:int):DisplayObject { for each (var gr:GridRow in g.getChildren()) { for each (var gi:GridItem in gr.getChildren()) { if (!(gi.getChildAt(0) is HBox)) { if (gi.getChildAt(0) is Text) { if (Text(gi.getChildAt(0)).id == String(id)) { return Text(gi.getChildAt(0)); } } if (gi.getChildAt(0) is TextInput) { if (TextInput(gi.getChildAt(0)).id == String(id)) { return TextInput(gi.getChildAt(0)); } } } } } return null; } hth. Kr. Luke. Jason B wrote: Because the items are not in MXML its in actionscript which dynamically creates the items for my form from a database and i want to dynamically loop the text box's so i can then save the data back to the database. you cant refer to a ID since the components are created at runtime --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Gordon Smith [EMAIL PROTECTED] wrote: Why are you trying to use getChildByName to get a reference to a component? If you give it an 'id' attribute in MXML, you can then refer to it by that id. Gordon Smith Adobe Flex SDK Team From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of Jason B Sent: Monday, August 18, 2008 8:07 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] getChildByName using it to get the value of a textbox when using getchildbyname variable i've not been able to get the value can someone please post an example of how to get a value using getChildByName. var test:DisplayObject = this.getChildByName(inputtext + i); -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo ! Groups Links -- Luke Vanderfluit Analyst / Web Programmer e3Learning.com.au 08 8221 6422