View comments | RSS feed

Selection.setFocus()

Availability

Flash Player 5. Instance names for buttons and movie clips work only in Flash Player 6 and later.

Usage

Selection.setFocus("instanceName":String)

Parameters

instanceName A string specifying the path to a button, movie clip, or text field instance.

Returns

A Boolean value; true if the focus attempt is successful, false if it fails.

Description

Method; gives focus to the selectable (editable) text field, button, or movie clip specified by instanceName. The instanceName parameter must be a string literal of the path to the instance. You can use dot or slash notation to specify the path. You can also use a relative or absolute path. If you are using ActionScript 2.0, you must use dot notation.

If null is passed, the current focus is removed.

Example

In the following example, the text field focuses on the username_txt text field when it is running in a browser window. If the user does not fill in one of the required text fields (username_txt and password_txt), the cursor automatically focuses in the text field that's missing data. For example, if the user does not type anything into the username_txt text field and clicks the submit button, an error message appears and the cursor focuses in the username_txt text field.

this.createTextField("status_txt", this.getNextHighestDepth(), 100, 70, 100, 22);
this.createTextField("username_txt", this.getNextHighestDepth(), 100, 100, 100, 22);
this.createTextField("password_txt", this.getNextHighestDepth(), 100, 130, 100, 22);
this.createEmptyMovieClip("submit_mc", this.getNextHighestDepth());
submit_mc.createTextField("submit_txt", this.getNextHighestDepth(), 100, 160, 100, 22);
submit_mc.submit_txt.autoSize = "center";
submit_mc.submit_txt.text = "Submit";
submit_mc.submit_txt.border = true;
submit_mc.onRelease = checkForm;
username_txt.border = true;
password_txt.border = true;
username_txt.type = "input";
password_txt.type = "input";
password_txt.password = true;
Selection.setFocus("username_txt");
//
function checkForm():Boolean {
   if (username_txt.text.length == 0) {
      status_txt.text = "fill in username";
      Selection.setFocus("username_txt");
      return false;
   }
   if (password_txt.text.length == 0) {
      status_txt.text = "fill in password";
      Selection.setFocus("password_txt");
      return false;
   }
   status_txt.text = "success!";
   Selection.setFocus(null);
   return true;
}

See also

Selection.getFocus(), Selection.onSetFocus

Comments


recoveredfromflashMX2004 said on Aug 4, 2004 at 4:13 PM :
Ajax said on Apr 29, 2004 at 8:09 PM :

If the method only takes a string parameter why does some of the sample code (e.g. UIComponent) use the method as:

Selection.setFocus( this);
vibetec said on Aug 26, 2004 at 12:22 PM :
It only works with me when I use Selection.setFocus( instancename )
without quotes.
Francis Cheng said on Aug 26, 2004 at 12:59 PM :
Thanks for bringing this up. The example works for me using either method, i.e. with or without quotes. I'll change the Usage section to reflect that the parameter can be either a String or an Object instance:
Selection.setFocus(instancePathName:String)
Selection.setFocus(instanceName:Object)
Cat1977 said on Sep 17, 2004 at 1:56 PM :
I'm using the Selection.setFocus method in a PopupManager window. The new window pops up, focus is set to the textArea, and you can start typing immediately, but there's no cursor showing up. How do I get the cursor to show up?
vyieort said on Oct 7, 2004 at 12:22 PM :
I'm not using the setFocus to actually set focus. When a user has entered some information in the text field and then is finished and saves some data, i run the setFocus(null) to remove focus from the text field that they voluntarily set focus on and entered information. i'm realizing that my problem exists when I setFocus(null) and then click anywhere else it returns the focus to the originating text field. any idea why that might be?
Francis Cheng said on Oct 15, 2004 at 8:59 AM :
LiveDocs is intended for documentation comments and does not receive enough traffic to handle questions like this effectively. I recommend that you post your issue to the online forums: http://webforums.macromedia.com/flash
Firetron said on Nov 15, 2004 at 11:06 AM :
This function dosn't always work properly when you test movie. It does work when running the swf in a browser or the stand alone player.
Real MaIron said on Feb 18, 2005 at 3:07 AM :
I,m find error in Macromedia code.

this.createTextField("one_txt", 99, 10, 10, 200, 20);
this.createTextField("two_txt", 100, 10, 50, 200, 20);
one_txt.border = true;
one_txt.type = "input";
two_txt.border = true;
two_txt.type = "input";

var focusListener:Object = new Object();
focusListener.onSetFocus = function(oldFocus_txt, newFocus_txt) {
oldFocus_txt.border = false;
newFocus_txt.border = true;
};
Selection.addListener(focusListener);
Selection.getFocus()

Work great !

But if I add to lib NumericStepper component.
When clicking on stage, focus still on a last selected text field.
mike6496 said on Mar 4, 2005 at 10:49 AM :
if publishing for Flash 5 you can't use the instance name of a text field.. you use the name of the variable associated with that text field..
son of godzilla said on Apr 27, 2005 at 3:58 AM :
the code in the example is to extensive you could have had just

this.createTextField("aTxt_1", 21, 100, 0, 100, 20);
aTxt_1.type = "input";
aTxt_1.border = true;
Selection.setFocus("aTxt_1");
Btn.onRelease = insertion;
function insertion() {
Selection.setFocus("aTxt_1");
}
No screen name said on Jul 28, 2005 at 10:24 PM :
Is there a method to remove the blinking cursor from a text field?

It seems that if the curor stays in the text field, and then if the user tries to click on some of my buttons, they don't work...
Maglez said on Aug 16, 2005 at 6:20 AM :
Using a TextInput component, 'myInputText', when I issue the Selection.setFocus from a button in the form...

on(click)
{
Selection.setFocus(_root.myInputText);
}

...it does set the focus on the TextInput component but at the same time it select the full content of the TextInput, all I want is to set the focus on it, with the caret blinking on its position.
Yahafiz said on Sep 1, 2005 at 6:19 AM :
Selection.setFocus(null) does deselect the text, but it doesn't remove the caret from the previously selected textfield. How do I do this?
Yahafiz said on Sep 1, 2005 at 6:25 AM :
Maglez said on Aug 16, 2005 at 6:20 AM

Use the Selection.setSelection(), just go to the next page...
mhumming said on Mar 6, 2006 at 9:33 PM :
Too get cursor blinking in a textField, without user having too first select the field try this.
_root.createTextField("txt",0,0,0,73,20);
var inp:TextField = _root.txt;
inp.type = "input";
inp.text = "enter txt"; // set some txt
Selection.setFocus(inp);
Selection.setSelection(0, 9); // select that text
inp.text = ""; // re-set txt
Cheers.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001658.html