4 Replies Latest reply on May 16, 2007 9:28 AM by dimival

    How to alter a button's enabled state from a script?

    BoyTheo Level 1
      How do I enable a button's enabled state from a script?

      The problem is, that I can't figure out how to reference that button!!! Let's say I have this script:


      pbPreviewTest.enabled() = lbTest.length >= 1;

      OK... well how do I get my script to be able to access pvPreviewTest and lbTest?

      I got this mxml...

      <mx:Button id="pbPreviewTest" label="Preview Test" right="5" top="5" enabled="false"/>

      and this also:

      <mx:DataGrid id="lbTest" height="413" width="100%" y="31" draggableColumns="false">

      but it doesn't seem to work :(
        • 1. Re: How to alter a button's enabled state from a script?
          ashar2005
          1. Put an mx:Script tag in your application to hold your function that will execute the line of code to enable the button.
          2. Create the function, in the example it is init()
          3. Datagrids do not have a "length" property, get the length of your datagrid's arraycollection
          or whatever you are using to populate the datagrid.
          4. Cast the result of the length property being >=1 as a Boolean value and set the button's property accordingly.

          Example below.... you should use the name of your arraycollection in place of lbTest.dataProvider in the example.


          <mx:Script>
          <![CDATA[
          public function init():void
          {
          pbPreviewTest.enabled = Boolean(lbTest.dataProvider.length >= 1);
          }
          ]]>
          </mx:Script>
          <mx:Button id="pbPreviewTest" label="Preview Test" right="5" top="5" enabled="false"/>
          <mx:DataGrid id="lbTest" height="413" width="100%" y="31" draggableColumns="false"/>
          • 2. Re: How to alter a button's enabled state from a script?
            BoyTheo Level 1
            Thanks, but the problem is it can't even access pbPreviewTest!

            pbPreviewTest.enabled() = true ;

            even that fails!

            Maybe it's because pbPreviewTest is embedded in another control?
            • 3. How to alter a button's enabled state from a script?
              BoyTheo Level 1
              This is my mxml, how do I access pbPreviewTest from the application script?

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Panel layout="absolute" top="0" bottom="0" left="0" right="0">
              <mx:Script>...
              </mx:Script>
              <mx:TextInput x="6" y="5" width="162"/>
              <mx:Button x="176" y="5" label="S" width="22"/>
              <mx:Button x="202" y="5" label="B" width="22"/>
              <mx:VDividedBox id="div1" x="0" y="35" height="100%" width="100%">
              <mx:HDividedBox width="100%" height="159">
              <mx:DataGrid id="lbPaths" initialize="lbPaths.dataProvider = acPaths" height="100%" sortableColumns="false" draggableColumns="false">
              <mx:columns>
              <mx:DataGridColumn headerText="Path" dataField="Name"/>
              </mx:columns>
              </mx:DataGrid>
              <mx:DataGrid id="lbView" height="100%" width="176" sortableColumns="false" draggableColumns="false">
              <mx:columns>
              <mx:DataGridColumn headerText="View" dataField="col3"/>
              </mx:columns>
              </mx:DataGrid>
              <mx:DataGrid id="lbQuestions" height="100%" draggableColumns="false">
              <mx:columns>
              <mx:DataGridColumn headerText="Title" dataField="col1"/>
              <mx:DataGridColumn headerText="Questions" dataField="col2"/>
              <mx:DataGridColumn headerText="Level" dataField="col3"/>
              <mx:DataGridColumn headerText="Apparatus" dataField="col3"/>
              </mx:columns>
              </mx:DataGrid>
              </mx:HDividedBox>
              <mx:Canvas id="cn1" width="100%" height="100%">
              <mx:Text x="3" y="7" text="Questions in Test:" width="209"/>
              <mx:DataGrid id="lbTest" height="413" width="100%" y="31" draggableColumns="false">
              <mx:columns>
              <mx:DataGridColumn headerText="No." dataField="col1"/>
              <mx:DataGridColumn headerText="Title" dataField="col2"/>
              <mx:DataGridColumn headerText="Question" dataField="col3"/>
              <mx:DataGridColumn headerText="Level" dataField="col4"/>
              <mx:DataGridColumn headerText="Apparatus" dataField="col5"/>
              </mx:columns>
              </mx:DataGrid>
              <mx:Button id="pbPreviewTest" label="Preview Test" right="5" top="5" enabled="false"/>
              </mx:Canvas>
              </mx:VDividedBox>
              <mx:Button x="300" y="5" label="Add"/>
              <mx:Button x="357" y="5" label="Remove"/>
              <mx:Button label="Preview Question" enabled="false" right="4" top="5"/>
              </mx:Panel>
              </mx:Application>
              • 4. Re: How to alter a button's enabled state from a script?
                dimival
                Remove the () from the enabled property:

                pbPreviewTest.enabled()= true // Doing this you are calling a method named enabled, and it is not a method it is a property

                instead do this:

                pbPreviewTest.enabled = true;