The id property really is only used when you create an object directly
in MXML, so it won't be of much use here.

One possible solution would be the use of an object as a hash table.
The code below demonstrates how you can do this (apologies if Yahoo's
text parser removes all the indenting):

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
        xmlns:mx="http://www.adobe.com/2006/mxml"; 
        layout="absolute">
        
        <mx:Script>
                <![CDATA[
                        import mx.controls.Text;
                        
                        protected var vBoxCollection:Object = {};
                        
                        protected function onDoClick():void
                        {
                                var boxName:String = tiBoxName.text;
                                var boxText:String = tiTextToAdd.text;
                                var vBox:VBox = null;
                                var textDisplay:Text = null;
                                
                                if(boxName != "")
                                {
                                        
if(!vBoxCollection.hasOwnProperty(boxName) ||
(vBoxCollection[boxName] == null))
                                        {
                                                vBox = new VBox();
                                                vBox.styleName = "theBoxStyle";
                                                vBoxCollection[boxName] = vBox;
                                                vbDisplay.addChild(vBox);
                                        }
                                        else
                                                vBox = vBoxCollection[boxName];
                                        
                                        if(vBox is VBox)
                                        {
                                                textDisplay = new Text();
                                                textDisplay.text = "Name: " + 
boxName + "\n" + boxText;
                                                vBox.removeAllChildren();
                                                vBox.addChild(textDisplay);
                                        }
                                }
                        }
                ]]>
        </mx:Script>
        
        <mx:Style>
                .theBoxStyle
                {
                        borderStyle: solid;
                        borderThickness: 2;
                        borderColor: #000000;   
                }
        </mx:Style>
        
        <mx:VBox 
                id="vbDisplay" 
                left="0" 
                top="0" 
                right="0" 
                bottom="30"
                verticalAlign="top" 
                horizontalAlign="center"/>
                
        <mx:HBox 
                id="hbControls"
                width="100%" 
                height="30" 
                bottom="0" 
                horizontalAlign="center" 
                verticalAlign="middle">
                
                <mx:Label 
                        text="Box Name:"/>
                        
                <mx:TextInput 
                        id="tiBoxName"/>
                        
                <mx:Spacer 
                        width="30"/>
                
                <mx:Label 
                        text="Text to Add:"/>
                        
                <mx:TextInput 
                        id="tiTextToAdd"/>
                        
                <mx:Spacer 
                        width="30"/>
                        
                <mx:Button 
                        label="Do It" 
                        click="onDoClick()"/>
                
        </mx:HBox>
        
</mx:Application>


Basically, give the box a name in the first field, then some text in
the second, and click the button. Once a box is created, its added to
vBoxCollection as a dynamic property, and can repeatedly be referenced
afterwards.

Try using this as a starting point. Hope it helps!

-- GC

--- In [email protected], "Anthony Ettinger" <[EMAIL PROTECTED]> wrote:
>
> Understandable, but I'm adding multiple VBoxes, so I'm giving them
> different IDs --- is it even possible to access it by the ID?
> 
> I want to create it, if it doesn't exists, and then write to it (the
> new one or the existing one).
> 
> 
> 
> On Wed, Apr 2, 2008 at 11:59 AM, caffeinewabbit
> <[EMAIL PROTECTED]> wrote:
> 
> > It sounds like you're approaching the problem from a JavaScript style
> >  mindset, which isn't really the best way to go when dealing with AS3.
> >
> >  Without knowing more about your project or surrounding code, I'd
> >  suggest something along these lines:
> >
> >  <?xml version="1.0" encoding="utf-8"?>
> >  <mx:Application
> >  xmlns:mx="http://www.adobe.com/2006/mxml";>
> >
> >  <mx:Script>
> >  <![CDATA[
> >
> >  protected var vBox:VBox = null;
> >
> >  protected function addVBox():void
> >  {
> >  if(!vBox)
> >  {
> >  vBox = new VBox();
> >  vBox.styleName = "theBoxStyle";
> >  this.addChild(vBox);
> >  }
> >  }
> >
> >  ]]>
> >  </mx:Script>
> >
> >  ... MXML code - possibly containing a component that calls
addVBox()...
> >
> >  </mx:Application>
> >
> >  In Flex, the id parameter really only needs to be set explicitly when
> >  a component is created in MXML. In AS3, the explicit variable name is
> >  used.
> >
> >
> >
> >  --- In [email protected], "Anthony Ettinger" <anthony@>
wrote:
> >  >
> >  > I want to dynamically create a VBox container for messaging from
> >  > ActionScript, however it's unclear to me how to check if one
already
> >  > exists given its ID.
> >  >
> >  > var vBox:VBox = new VBox();
> >  >
> >  > vBox.id = 'theBox";
> >  > vBox.styleName = "theBoxStyle";
> >  >
> >  > addChild(vBox); // here is where I want to check for
getById('theBox')
> >  > in the DOM before adding a new one.
> >  >
> >  >
> >  >
> >  >
> >  >
> >  > --
> >  > Anthony Ettinger
> >  > 408-656-2473
> >  > http://anthony.ettinger.name
> >  >
> >
> >  
> 
> 
> 
> -- 
> Anthony Ettinger
> 408-656-2473
> http://anthony.ettinger.name
>


Reply via email to