AS2
I have some as-generated input fields that need to have default values (already filled out) that go away when the user selects the field, and then come back if the field loses focus with no input - value of "".
I can't for the life of my get it to work right. Here's what I've got:
(please excuse my probably laughable attempt at a class - I'm still new to doing things this way)
import mx.utils.Delegate; class com.client.content.ui.InputField extends MovieClip { // private vars private var mcBG:MovieClip; private var mcText:TextField; private var mcError:MovieClip; private var controllerTarget:MovieClip; private var bgLength:Number = 265; private var maxInput:Number = null; private var inputType:String = "all"; private var multiLine:Boolean = false; private var mouseListener:Object = new Object();
// public vars
public var introText:String = null;
public var inputValue:String = null;
public function InputField(){};
private function onLoad():Void{};
public function initInput(controllertarget:MovieClip, introtext:String, bglength:Number, maxinput:Number, inputtype:String, txtmultiline:Boolean):Void{
controllerTarget = controllertarget;
introText = introtext;
if (bglength != undefined && bglength != null){
bgLength = bglength;
}
if (maxinput != undefined && maxinput != null){
maxInput = maxinput;
}
if (inputtype != undefined && inputtype != null){
inputType = inputtype;
}
if (txtmultiline != undefined && txtmultiline != null){
multiLine = txtmultiline;
}
buildInput();
};
private function buildInput():Void{
mcBG._width = bgLength;
if (multiLine){
mcBG._height = 40;
}
var bgColor:Color = new Color(mcBG);
bgColor.setRGB(_root.skinArray["content"]["selectListBG"]);
mcText = this.createTextField("inputtext", this.getNextHighestDepth(), 7, 0, bgLength-15, mcBG._height);
mcText.autoSize = false;
mcText.antiAliasType = "advanced";
mcText.background = false;
mcText.backgroundColor = null;
mcText.selectable = true;
mcText.type = "input";
mcText.embedFonts = true;
if (multiLine){
mcText.multiline = false;
mcText.wordWrap = false;
} else {
mcText.multiline = false;
mcText.wordWrap = false;
}
mcText.html = false;
mcText.text = introText;
_root.scpHelveticaInput.color = _root.skinArray["content"]["selectListText"];
mcText.setTextFormat(_root.scpHelveticaInput);
// set any restrictions on the field input type
if (inputType != "all"){
switch (inputType){
case "letters":
mcText.restrict = "A-Z";
break;
case "numbers":
mcText.restrict = "0-9";
break;
case "phone":
mcText.restrict = "0-9().x+ \\-";
break;
}
}
mcText.onSetFocus = Delegate.create (this, textFieldSetFocus);
mcText.onKillFocus = Delegate.create (this, textFieldKillFocus);
};
private function textFieldSetFocus():Void{
Mouse.addListener(mouseListener);
mouseListener.onMouseDown = Delegate.create (this, mouseClickTest);
mcText.type = "input";
if (mcText.text == introText){
mcText.text = "";
}
mcText.setTextFormat(_root.scpHelveticaInput);
Selection.setSelection(0, mcText.text.length);
};
private function textFieldKillFocus():Void{
Mouse.removeListener(mouseListener);
if (mcText.text == ""){
mcText.text = introText;
}
mcText.type = "dynamic";
mcText.setTextFormat(_root.scpHelveticaInput);
};
}
What happens right now when I select the field is that the text is cleared out, but the field becomes inactive - I can't click in it, I can't type in it, I can't do anything.
If I don't set the value to be blank:
if (mcText.text == introText){ mcText.text = "foo"; }
Then it sort of works (albeit with some dummy text in there). It will replace the introText value with foo, and allow me to type in the field.
The mouse listener is in there because without it the user clicking in the space outside of the field doesn't make the field lose focus. And while using the dummy text as the replacement, the mouse listener successfully triggers the textFieldKillFocus() method, the text doesn't get replaced back to the introText value (even when I change the if to check for '== "foo"' and once the field has lost focus, it's no longer able to gain focus.
What am I doing wrong?
PS - my class extends movieclip rather than textField because there are a lot of non-text things that need to happen in it, in addition to what's shown here. Plus, I get Movie Clip classes, whereas my success with extending other types of objects is more hit an miss. Like I said, I'm new to OOP.
from what you say it sounds like it doesn't like running these two lines:
mcText.setTextFormat(_root.scpHelveticaInput);
Selection.setSelection(0, mcText.text.length);
on an empty field?
Try putting them in an else block, so that it either clears the text, or it does the setTextFormat and setSelection lines.
Also, not sure why you have to use setTextFormat all the time - isn't it enough to have done that when you created the input field in the first place?
I put it in the additional times, thinking maybe the problem was changing the text after the formatting had been applied - thinking I needed to re-apply the format when the content had changed.
Didn't make much sense then, or now 
I'll give your suggestion a try, thanks.
No dice.
If I don't include setTextFormat and setSelection in the textFieldSetFocus() method, then it doesn't work at all (not that it really works to begin with). If I set the value to be "" on focus, then the field isn't selectable.
It's stuff like this that makes me hate Flash. This would have taken me 10 seconds in html, but I've spent hours on it so far in Flash.
sorry without actually reading did you try using on change event?
I was using onSetFocus and onKillFocus.
I'll give onChanged a try though and see if I can get anywhere with that.
Thanks for the suggestion 