# Loading items from a text file into a listbox

**URL:** <https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718>\
**Category:** Uncategorized\
**Created:** [June 7, 2003, 4:38pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718 "2003-06-07T16:38:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![breakpoint](https://avatars.discourse-cdn.com/v4/letter/b/ac8455/32.png) [@breakpoint](https://forum.kirupa.com/u/breakpoint)\
**Post date:** [June 7, 2003, 4:38pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/1 "2003-06-07T16:38:32Z")

</div>

As the title suggest I am wondering how do you load the contents of a text file into a listbox component. My text file is formatted like this.

item1  
item2  
item3  
…  
…  
…

Thx in advance…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:17pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/2 "2003-06-07T17:17:16Z")

</div>

well, although i’ve never used a component, i assume it would be something like this:

```auto
var myLoadVars = new LoadVars();
myLoadVars.onLoad = function(success) {
	if (success) {
		var contents = this.contents.split(","), index;
		for (index=0; index<contents.length; index++) {
			myFListBox.addItem(contents[index]);
		}
	} else {
		trace("error loading contents");
	}
};
myLoadVars.load("contents.txt");

```

and your .txt file should read:

```auto
contents=item1,item2,item3,item4&amp;

```

hope it helps… 😛 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:22pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/3 "2003-06-07T17:22:13Z")

</div>

Kax

thx for the help. Only problem I have is the formatting of the text file has to stay as it is (one item per line) so is there any way that your code could be modded for that ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:28pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/4 "2003-06-07T17:28:28Z")

</div>

ehmm… try this:

```auto
var myLoadVars = new LoadVars();
myLoadVars.onLoad = function(success) {
	if (success) {
		var contents = unescape(this).split("
"), index;
		for (index=0; index<contents.length; index++) {
			myFListBox.addItem(contents[index]);
		}
	} else {
		trace("error loading contents");
	}
};
myLoadVars.load("contents.txt");
ASSetPropFlags(myLoadVars, "onLoad", 1);

```

:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:37pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/5 "2003-06-07T17:37:33Z")

</div>

wait… now that i think about it, there will be an assignment operator (=\*\*\*\*) in the last item, try this instead:

```auto
var myLoadVars = new LoadVars();
myLoadVars.onLoad = function(success) {
	if (success) {
		var str = unescape(this), contents = str.substr(0, str.length-1).split("
"), index;
		for (index=0; index<contents.length; index++) {
			myFListBox.addItem(contents[index]);
		}
	} else {
		trace("error loading contents");
	}
};
myLoadVars.load("contents.txt");
ASSetPropFlags(myLoadVars, "onLoad", 1);

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:50pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/6 "2003-06-07T17:50:30Z")

</div>

were getting there…

One last problem is if the item has an ampersand character in it then the list gets messed up so how would I escape the “&” character so the formatting stays as it is in the text file and thx again for the help.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 5:52pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/7 "2003-06-07T17:52:34Z")

</div>

**%26** 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:14pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/8 "2003-06-07T18:14:26Z")

</div>

thanks again for the help and I am sure your asking yourself “can’t this guy do anything for himself” but I was wondering if there was a way to escape the “&” from within the code and not in the text file itself ?

If not or it would be too difficult to code then I understand and thanks alot again.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:24pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/9 "2003-06-07T18:24:53Z")

</div>

you’re welcome. 😉

and no, i don’t think so (at least i can’t think of anything right now), you’ll have to escape the ampersand in the .txt file. :-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:49pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/10 "2003-06-07T18:49:03Z")

</div>

What kind of title is that, kax?  
you’re

```auto
Sen.removeListener(pom);

```

'ing me?  
:trout:  
That’s for you

```auto
function ClearKax (clip){
for (var i in clip){
if (typeof (clip*) == Kax) delete clip*;
else ClearKax(clip*);
}
}
ClearKax(_root);

```

:evil:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:51pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/11 "2003-06-07T18:51:34Z")

</div>

what kind of title is that, ilyas!? :hair:

senocular is mine and only mine!! :trout:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:52pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/12 "2003-06-07T18:52:25Z")

</div>

No!!! ☹

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 7, 2003, 6:53pm UTC](https://forum.kirupa.com/t/loading-items-from-a-text-file-into-a-listbox/22718/13 "2003-06-07T18:53:39Z")

</div>

ehem… you edited your post… 😛

and yes!! he’s mine!! :bad:
