views:

957

answers:

2

Hello!
I made a Form container with few TextInput fields in Flex and I would like to change a style of clicked TextInput element. I managed to do it with MouseEvent.CLICK event listener but it is not what I expected.
I would like to change style of TextInput when user try to edit that field. However, I want to change back to 'default' style when user clicks another TextInput field or outside any field. Is there some event listener made specific for such interactions?

I have also a question, is it possible to make width of TextInput depending on amount of text? I mean to resize it to width of text inside of it? Text comes from some database and is always different widths...

Thanks for help!

+2  A: 

Instead of using click events, use "focus" and "blur" events. This way you'll pick up on the event as the user clicks (or tabs) to the field, and you'll also pick up on the event when they leave the field.

JasonWyatt
Thank you! I solved the problem with FocusEvent.FOCUS_IN and FocusEvent.FOCUS_OUT listeners...
errata
+1  A: 

As to your second question. If you don't set an explicit width on the text input I would think it would use flash.text.TextLineMetrics to calculate its optimum width automatically. Check the measure method in the code for the TextInput to verify.

Ryan Lynch
Thanks for help... I did try some of measuring methods and properties, but none of them is working =( When I try to use measureText() on my TextInput field, I get this error: TypeError: Error #2007: Parameter antiAliasType must be non-null. Also, I see in debugger that measureWidth returns 0... Hmmm, why is this happening?
errata
I don't mean call the `measure()` method, I meant look at the code to see what the control does if you don't set an `explicitWidth`.
Ryan Lynch
hmm, I'm not sure if I understood you right, but I checked also explicitWidth and its value is NaN, just as all other explicitMin/explicitMax Widths/Heights...
errata
I'll take a look at the `TextInput`'s `measure` function later and see if you'll need to extend the class and override the method to make it auto size to text. I would think there is public flag you can set on the `TextInput` which would cause the control auto-size to text, or that it would be the default behavior if you don't set a `width` on the control.
Ryan Lynch
Still no luck with this... I have seen in TextInput.as internal class that I should call validateNow() before I could get textWidth value, but I always get NaN for textWidth and 0 for all other properties (width, measuredWidth, etc.)...
errata
Uf, ok, I managed to solve my problem in a way to create creationComplete listener for TextInput components and resize them there... Thanks for tips =)
errata