2 Replies Latest reply on Sep 4, 2006 9:50 AM by el Fakir

    Conditional RemoveChild?

    el Fakir Level 1
      Hello there!

      I've created a search form based on a canvas (id="searchCanvas"), which is hidden by default. A button is used to display it (or hide it again), according two defined states "hiddenSearch" and "visibleSearch", with "hiddenSearch" as the default currentState.

      An error is raised when my application starts, which is quite normal because the default state tries to remove an unexisting element. Of course, I'd like to fix that.

      So, here you are my question: how can I rewrite my "hiddenSearch" state to only call the <mx:RemoveChild> if the "searchCanvas" exists?

      Thanks a lot for your help,
      Anthony --

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" currentState="hiddenSearch">
      <!-- States -->
      <mx:State name="hiddenSearch">
      <mx:SetEventHandler target="{btnSearch}" name="click" handler="currentState='visibleSearch'"/>
      <mx:SetProperty target="{tableCanvas}" name="height" value="460"/>
      <mx:SetProperty target="{tableCanvas}" name="y" value="20"/>
      <!-- Because searchCanvas hasn't been yet created, RemoveChild doesn't work the first time. -->
      <mx:RemoveChild target="{searchCanvas}"/>

      <mx:State name="visibleSearch">
      <mx:AddChild relativeTo="{mainCanvas}" position="firstChild">
      <!-- Search form component, defined inside another .mxml file. -->
      <local:searchFormComponent id="searchCanvas" y="20"/>
      <mx:SetEventHandler target="{btnSearch}" name="click" handler="currentState='hiddenSearch'"/>
      <mx:SetProperty target="{tableCanvas}" name="height" value="240"/>
      <mx:SetProperty target="{tableCanvas}" name="y" value="240"/>

      <!-- Elements -->
      <mx:Canvas label="All" width="100%" height="100%" id="mainCanvas">

      <mx:Button id="btnSearch" label="Show/hide search" x="0" y="0"/>

      <mx:Canvas height="200" width="100%" y="200" id="tableCanvas">
      <!-- Table code there, removed for better lisibility. -->