I am a Flex newbie and I am testing a little application that simulates a cart. (It is based on a nice sample by Farata Systems' Yakov Fain). Note: I am using the beta of Flash Builder 4 to code the application.
Here you have a link to the screenshot: Screenshot
(Sorry I can't insert the image of the screenshot right here since stackoverflow doesn't allow new users to use image tags.)
The application is very simple. You type a product in the TextInput control and then click on the "Add to cart" button to add it to the "cart" which is represented by the TextArea at the bottom.
That works ok.
The problem is that I also want the user to be able to keep adding items to the cart without having to click on the "Add to cart" button. So, I added code to handle the enter event of the TextInput by calling the same handler function triggered by the "Add to cart" Click event.
If you type some content and then click the "Add to cart" button, the TextInput control receives the focus and the cursor, so you can type again. However, if you hit enter, instead of clicking the button, the TextInput control keeps focused and you can see the cursor beam, but you can not enter text until you click elsewhere and come back to the control.
Below you can see the relevant part of the code, with the definition of the component that groups the three controls at the top (Label, TextInput, Button).
¿Any suggestions?
<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
<![CDATA[
protected function newItemHandler():void
{
import cart.ItemAddedEvent;
var e : ItemAddedEvent = new ItemAddedEvent( "->" + textInputItemDescription.text );
textInputItemDescription.text = "";
textInputItemDescription.setFocus();
textInputItemDescription.setSelection(0,0);
dispatchEvent( e ); // Bubble to parent = true
trace( "I just dispatched AddItemEvent " + e.toString() + "\n Content = " + e.itemDescription );
}
]]>
</fx:Script>
<fx:Metadata>
[Event(name="ItemAddedEvent",type="cart.ItemAddedEvent",bubbles=true)]
</fx:Metadata>
<mx:Label text="Item description:"/>
<s:TextInput id="textInputItemDescription" enter="newItemHandler() "/>
<s:Button click="newItemHandler()" label="Add to cart"/>