Skip to main content
Inspiring
July 21, 2009
Answered

Adding filename of a file to a textbox

  • July 21, 2009
  • 1 reply
  • 748 views

Hi,

How would I  add the name of an item in an array to a text box? I want to add the name of each item in an array to the text box depending which picture is being displayed.

Here is the code:

var iholder:Loader = new Loader();//create the loader object

//set up the event handler to add the content to the display list when the content has finished loading
iholder.contentLoaderInfo.addEventListener(Event.COMPLETE, contentLoaded);
function contentLoaded(event:Event):void {
this.addChild(iholder);
}

var images:Array = new Array("image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.jpg", "image6.jpg", "image7.jpg","image8.jpg", "image9.jpg", "image10.jpg");
var imagefolder:String = "images/"; //Remove the "/" before the folder name for the splash page.
var imagenum:int = 0;
var url:String = imagefolder + images[imagenum];
var imagereq:URLRequest = new URLRequest(url);
var holders:Array = new Array();
var mainHolder:Loader = new Loader();
this.addChild(mainHolder);
//Large image
mainHolder.x = 100;
mainHolder.y = 40;
var slider:Sprite = new Sprite();
this.addChild(slider);
holders[imagenum] = iholder;//copy into holders array for easy access
holders[imagenum].load(imagereq);//load first thumnail
function clickHandler(event:MouseEvent) {
//get the filename of the jpeg whose thumbnail was clicked
var url:String = event.target.contentLoaderInfo.url;
var imagereq:URLRequest = new URLRequest(url);
mainHolder.load(imagereq);//load the jpeg whose thumbnail was clicked
}
function imagesLoaded(event:Event) {
holders[imagenum].width=60;//resize image to thumbnail size
holders[imagenum].height = 60;
holders[imagenum].x= imagenum *(0);//position
holders[imagenum].y= imagenum *(32);//position
holders[imagenum].addEventListener(MouseEvent.CLICK, clickHandler);
slider.addChild(holders[imagenum]);
// now load the next one (if there are more) or set up handlers if done
imagenum++;
if (imagenum<images.length) {//if there are more images
var newurl = imagefolder+images[imagenum];//get the new url
var newreq: URLRequest = new URLRequest(newurl);
var newholder:Loader = new Loader();//create a new loader to load next jpeg
holders[imagenum] = newholder;//copy new loader into holders array
holders[imagenum].load(newreq);//load new jpeg
holders[imagenum].contentLoaderInfo.addEventListener(Event.COMPLETE, imagesLoaded);
}
}
holders[imagenum].contentLoaderInfo.addEventListener(Event.COMPLETE, imagesLoaded);
//set up the sliding function
this.addEventListener(Event.ENTER_FRAME, doEveryFrame);
function doEveryFrame(event:Event):void {
//create a variable to check if the RHS of the slider is at the rhs of stage
var tooFarLeft = (this.slider.width-800)*-1;
//If mouse is over the left side of the slider and not too far left move left
if ((slider.mouseX<slider.width/2) && (slider.x>tooFarLeft)) {
slider.x -= 2;
//else if mouse over the rhs of slider and not too far right move right
} else if ((slider.mouseX>slider.width/2) && (slider.x<0)) {
slider.x += 2;
}
}

So if say image1 is displayed the filename of that image will appear in a textbox on the stage.

Thanks,

Nightwalker

This topic has been closed for replies.
Correct answer Ned Murphy

Assuming you have a textfield named "tf"... modify your clickHandler function as follows:

function clickHandler(event:MouseEvent) {

//get the filename of the jpeg whose thumbnail was clicked

var url:String = event.target.contentLoaderInfo.url;
var imagereq:URLRequest = new URLRequest(url);
mainHolder.load(imagereq);//load the jpeg whose thumbnail was clicked

//display the filename of the jpeg whose thumbnail was clicked

tf.text = String(images[holders.indexOf(event.currentTarget)]);
}

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 21, 2009

Assuming you have a textfield named "tf"... modify your clickHandler function as follows:

function clickHandler(event:MouseEvent) {

//get the filename of the jpeg whose thumbnail was clicked

var url:String = event.target.contentLoaderInfo.url;
var imagereq:URLRequest = new URLRequest(url);
mainHolder.load(imagereq);//load the jpeg whose thumbnail was clicked

//display the filename of the jpeg whose thumbnail was clicked

tf.text = String(images[holders.indexOf(event.currentTarget)]);
}

Inspiring
July 22, 2009

Thanks! It work great.