RE: [flexcoders] Using Container.addChild(), can we not assign the id property and use it to refrence the new control?

2006-11-27 Thread Gordon Smith
Since most classes in the Flex framework are non-dynamic, assigning the
value canvas1 to the 'id' property at runtime can't create an instance
variable called canvas1 that contains a reference to your new Canvas,
which is what the MXML compiler does when it processes the id=canvas1
attribute at compile time.

 

However, you can simply declare this instance variable yourself:

 

private var canvas1:Canvas;

 

private function addCanvas(oEvent:Event):void

{

canvas1 = new Canvas();

canvas1.width = 300;

canvas1.height = 100;

addChild(canvas1);

}

 

private function addControlToCanvas(oEvent:Event):void

{

var textAreaNew:TextArea = new TextArea();

textAreaNew.text = Hello, Other World!;

canvas1.addChild(textAreaNew);

}

 

This is the recommended technique. We don't recommend using
getChildByName() to find dynamically created components.

 

- Gordon

 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Tracy Spratt
Sent: Monday, November 27, 2006 1:49 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Using Container.addChild(), can we not assign the
id property and use it to refrence the new control?

 

When I try, I get an error that the control is not a property of the
app.  For example:

I add a Canvas, and set the ID

  private function addCanvas(oEvent:Event):void

  {

caNew = new Canvas();

caNew.id = canvas1

caNew.name = canvas1;

caNew.width = 300;

caNew.height = 100;

this.addChild(caNew);

  }//  

  

//I try to add a control to the canvas

  private function addControlToCanvas(oEvent:Event):void

  {

var TextAreaNew:TextArea = new TextArea();

TextAreaNew.text = Hello, Other World!

//Of course, we can't use the id directly because it does not exist
at compile time, but

//The next line, using bracket notation on the id of the canvas
still does not find the instance.

//is the bracket notation getting evaluated at compile-time, that
would explain it.

this[canvas1].addChild(TextAreaNew);

//The following two comment lines show a method that DOES work 

//var caTemp:Canvas = Canvas(this.getChildByName(canvas1));

//caTemp.addChild(TextAreaNew);

  }//  

So is it possible to assign and use an id for a dynamically instantiated
control instance?  Or is getChildByName the best solution?

Tracy

 



RE: [flexcoders] Using Container.addChild(), can we not assign the id property and use it to refrence the new control?

2006-11-27 Thread Tracy Spratt
I get it, thanks!

Tracy

 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Gordon Smith
Sent: Monday, November 27, 2006 5:45 PM
To: flexcoders@yahoogroups.com
Subject: RE: [flexcoders] Using Container.addChild(), can we not assign
the id property and use it to refrence the new control?

 

Since most classes in the Flex framework are non-dynamic, assigning the
value canvas1 to the 'id' property at runtime can't create an instance
variable called canvas1 that contains a reference to your new Canvas,
which is what the MXML compiler does when it processes the id=canvas1
attribute at compile time.

 

However, you can simply declare this instance variable yourself:

 

private var canvas1:Canvas;

 

private function addCanvas(oEvent:Event):void

{

canvas1 = new Canvas();

canvas1.width = 300;

canvas1.height = 100;

addChild(canvas1);

}

 

private function addControlToCanvas(oEvent:Event):void

{

var textAreaNew:TextArea = new TextArea();

textAreaNew.text = Hello, Other World!;

canvas1.addChild(textAreaNew);

}

 

This is the recommended technique. We don't recommend using
getChildByName() to find dynamically created components.

 

- Gordon

 



From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Tracy Spratt
Sent: Monday, November 27, 2006 1:49 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Using Container.addChild(), can we not assign the
id property and use it to refrence the new control?

 

When I try, I get an error that the control is not a property of the
app.  For example:

I add a Canvas, and set the ID

  private function addCanvas(oEvent:Event):void

  {

caNew = new Canvas();

caNew.id = canvas1

caNew.name = canvas1;

caNew.width = 300;

caNew.height = 100;

this.addChild(caNew);

  }//  

  

//I try to add a control to the canvas

  private function addControlToCanvas(oEvent:Event):void

  {

var TextAreaNew:TextArea = new TextArea();

TextAreaNew.text = Hello, Other World!

//Of course, we can't use the id directly because it does not exist
at compile time, but

//The next line, using bracket notation on the id of the canvas
still does not find the instance.

//is the bracket notation getting evaluated at compile-time, that
would explain it.

this[canvas1].addChild(TextAreaNew);

//The following two comment lines show a method that DOES work 

//var caTemp:Canvas = Canvas(this.getChildByName(canvas1));

//caTemp.addChild(TextAreaNew);

  }//  

So is it possible to assign and use an id for a dynamically instantiated
control instance?  Or is getChildByName the best solution?

Tracy