Listbox, ScrollPane, and Textbox

I have a little question:

A listbox with the instance name “list”, a dynamic textbox with the instance name “infotb”, and a scrollpane with the instance name “imgsp”.

Now, using this AS:
[AS]var myLoadVars = new LoadVars() ;
myLoadVars.onLoad = function(success) {
if (success) {
var str = unescape(this), infolist = str.substr(0, str.length-1).split("
"), index ;
for (index = 0 ; index < infolist.length ; index++) {
list.addItem(infolist[index]) ;
list.sortItemsBy(fieldName, asc) ;
}
} else {
trace(“Error loading contents!”) ;
}
} ;

myLoadVars.load(“infolist.txt”) ;
ASSetPropFlags(myLoadVars, “onLoad”, 1) ;[/AS]
which Kax posted in a thread some time ago, I am able to use a textfile to add items to the listbox (each line of text in the text file is a new item in the listbox).

However, I am unsure of what AS would be required so that two other values are read from the text file for each item. Those two values would be i) the name of an image [for the scrollpane imgsp] and ii) the name of a textfile [to be loaded into the dynamic textbox infotb].

e.g. One line of the text file could be:
JK87 [myimg.jpg, mytext.txt]

Or similar. When Flash reads the text file, it adds the item JK87 to the listbox and takes those two file names and if that item of the listbox (JK87) is selected, tells the scrollpane to display myimg.jpg and tells the dynamic textbox to display the text in mytext.txt

Could somebody please help me? Pretty please?(-:

Thanks for any and all help,
JK.

Wouldnt be better to use XML in that case?
Just a guess
:-\

I have absolutely no idea how to use XML lol. Or mySQL (with Flash or PHP) for that matter. I read the introduction tutorial for XML but it didn’t really sink in.

Could you give me an example of what I would need to use (the AS and the XML) in order to do this using XML please? :).

Man i suck at XML. Maybe someone more experienced can help you with that.
:slight_smile:

Heh ok thx for the help so far :).

looks around for somebody who is more experienced with combining XML and Flash(-:

Just to get things rolling on the XML side of things, I looked through the tutorials again and came up with this XML so far:


<USERS>
	<JK87 IMAGE="jk87.jpg" NAME="Jonathan King" AGE="15" />
	<Two IMAGE="two.jpg" NAME="T W O" AGE="16" />
</USERS>

Bear in mind, I don’t know the most efficient way of using XML but I thought that if I created a node for each listbox item, that might be a sensible way of doing things.

Therefore in the XML above, the listbox items would be JK87 and Two. And then the image displayed would be jk87.jpg or two.jpg depending on which listbox item is selected. And the extra information (name and age) would be entered into the textbox.

Could somebody please check this and explain to me how on Earth I get Flash to use the XML in the way I described?

Thanks soooo much,
JK :beam:.

Hey JK, can I take a look at the fla and the txt file?

Sure but there isn’t an actual text file atm… In the .fla all there is a textbox, a listbox, a scrollpane and some AS. But here you go :slight_smile: : [download link removed].

I imagine that the only external file required would be the .xml file with the information I posted above (the XML code).

[note: The current AS in the .fla is designed to work with a .txt file called infolist.txt and it basically adds each line of text in the text file as a new listbox item. I guess that would need to change when XML is used]

Thanks,
JK.

I think got a way around this… i used a different approach than Kax. I used 2 txt files, one holding the listbox itens and another with the sub data info.
Heres the code i used:[AS]var loadText = new LoadVars();
loadText.load(“infolist.txt”);
loadText.onLoad = function(success) {
if (success) {
var listArray = loadText.listdata.split(’,’), i; //split the content of txt file into an array
for (i=0; i<listArray.length; i++) {
list.addItem(listArray*); //pass the values stored in the array to the listbox
}
} else {
list.addItem(“Error loading contents!”);
}
};
list.setChangeHandler(“myHandler”);
function myHandler(component) {
var which = list.getSelectedItem().label;//set which item is selected
var subData = new LoadVars();
subData.load(“sublist.txt”);
subData.onLoad = function(success) {
if (success) {
contenttb.text = this[which];//search the txt file for the correspondent info
imgsp.loadScrollContent(which+".jpg");//load correspondent image
} else {
contenttb.text = “Error loading sub data!”;
}
};
}
[/AS]
Hope it helps.

Does a little dance

All hail claudio, KING of ActionScript :beam: :beam:.
Thankyou soooo much claudio :).

Oh and btw, I love the images you used for testing :thumb: ;).

Ahhh glad i could help you =)
And those images were reallly crap :stuck_out_tongue: i was too lazy to search google…

Just in case you want the listbox options to be displayed in alphabetic order, use:
[AS]var loadText = new LoadVars();
loadText.load(“infolist.txt”);
loadText.onLoad = function(success) {
if (success) {
var listArray = loadText.listdata.split(’,’), i; //split the content of txt file into an array
listArray.sort();//sort itens in alphabetic order (descend)
for (i=0; i<listArray.length; i++) {
list.addItem(listArray*); //pass the values stored in the array to the listbox
}
} else {
list.addItem(“Error loading contents!”);
}
};
list.setChangeHandler(“myHandler”);
function myHandler(component) {
var which = list.getSelectedItem().label;//set which item is selected
var subData = new LoadVars();
subData.load(“sublist.txt”);
subData.onLoad = function(success) {
if (success) {
contenttb.text = this[which];//search the txt file for the correspondent info
imgsp.loadScrollContent(which+".jpg");//load correspondent image
} else {
contenttb.text = “Error loading sub data!”;
}
};
}[/AS]

Thanks :thumb:. Ooh btw, I’ve been trying to do it with XML as well (just for future reference) and I’m so darn close lol. Your way is almost certainly easier for a small-scale operation like the one that I am doing atm but I intend to do it on a much larger scale in the future, which means I need to try it with XML.

So far, I’ve worked out how to display the following:
[AS]
//Number of users:
myXML.firstChild.childNodes.length
//Name of user (replace n with a node number):
myXML.firstChild.childNodes[n].nodeName
//Data of user (replace i with a node number):
myXML.firstChild.childNodes*.childNodes[0].nodeValue
//Display data in contenttb (replace i with a node number):
contenttb.text = myXML.firstChild.childNodes*.childNodes[0].nodeValue[/AS]

When the XML data is specified in Flash by using:
[AS]myXML = new XML( ‘<USERS><JK87>Nick: JK87
Name: Jonathan King
Age: 15</JK87>’ +
‘<Two>Nick: 2
Name: 2
Age: 2</Two>’ +
‘<Three>Nick: 3
Name: 3
Age: 3</Three></USERS>’) ;[/AS]

I would definetely help you if i knew XML. :slight_smile:
I guess its time for me to learn that…
:beam:

Heh yeah, me too :). No time like the present! ;).

I’ve got the user nicks displaying in the listbox now. I just have to work out the next bit regarding how each selection in the listbox affects what is displayed in the textbox.

Man… it’s a lot of work when you have no idea what you’re doing (-:.

Yea i know the feeling :slight_smile:
Good luck for you… let me see the final result.

Well, I’ve got guests coming over tonight so I’m probably going to have to stop until tomorrow. So far I have the names displaying in the listbox (haven’t set an error thing in-case they can’t load) but I just can’t work out how to get the textbox to work in coordindation with what is selected in the listbox.

I used trace to test “list.getSelectedItem().label” and it returned “undefined”. I would expect that this is being caused by the way I told Flash to add the listbox items (users). I can’t really see a way round it right now - I tried using index values for the listbox items; still “undefined”. I even tried changing what is entered so that each item had a data value (“list.getSelectedItem().data” to see what data you added) as well as its label but it still came up as “undefined”.

I’m lost for now but I will have another whack tomorrow. If you have any questions about what I have done so far, just post here and I’ll try and pop back when our guests aren’t looking :).

Here’s the .fla so far: http://www.modulus.plus.com/87xml.fla
And the XML is actually set using AS in the .fla so there are no external documents for the moment (I was having problems using an external .xml file to store the XML data).

Thanks for any help you can offer,
JK.

Well, it’s 00:15 am, our guests have gone and I have absolutely no idea how to get the last bit of this to work lol. If anybody could help me out, that would be great :beam:.

Thanks,
JK.

Also, claudio, how would I change the following so that if no image file was found, it displays a default image (e.g . noimage.jpg) ?
[note: this is from the .fla you created which uses textfiles rather than XML]
[AS]imgsp.loadScrollContent(which+".jpg");//load corresponding image[/AS]

I’m not sure as to how you check if which+".jpg" exists. If somebody can explain that, it would be great because then all that would be required would be along the lines of:

if (insert check if which+.jpg exists == true){
imgsp.loadScrollContent(which+".jpg");
}else{
imgsp.loadScrollContent("noimage.jpg");
}
 

Thanks again,
JK.

Hmmm… after a little more rigorous searching, I managed to find a thread in which my question about how to check whether a file is present is pretty much answered. For those of you who’re interested:
http://www.kirupaforum.com/forums/showthread.php?s=&threadid=9872&highlight=check+if+a+file+exists