Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 01-18-2006, 04:53 PM   #1
msimmons
Senior Member
 
msimmons's Avatar
 
Join Date: Jun 2005
Posts: 153
Default Having trouble selecting a combo box item

I am loading a mc that has a combo box on it and I want to pre select an item... this combo is built from the database via xml. an example of the data would be:

index : data : label
0 : 123456 : john doe
1 : 123457 : jane doe
2 : 123458 : someone else

and so on...

so I have the "123456" part and I want to pre select it... how?
thanks
msimmons is offline   Reply With Quote
Old 01-18-2006, 04:55 PM   #2
flashead
daehsalf
 
Join Date: Feb 2005
Location: Toronto, ON
Posts: 1,420
Default

instanceNameOfComboBox.setSelectedIndex(number);
And pass a number, 0 being the first item in your combo, 1 being the 2nd etc.

Last edited by flashead; 01-18-2006 at 04:57 PM.
flashead is offline   Reply With Quote
Old 01-18-2006, 04:59 PM   #3
msimmons
Senior Member
 
msimmons's Avatar
 
Join Date: Jun 2005
Posts: 153
Default

i could have done that but...
I do not know what number 612617 corresponds to. The combo is populated from a database so I do not know how many records or at what position they will be at design time.
msimmons is offline   Reply With Quote
Old 01-19-2006, 04:28 AM   #4
flashead
daehsalf
 
Join Date: Feb 2005
Location: Toronto, ON
Posts: 1,420
Default

oh.
now that i've read your question a second time, i think i understand what you're asking.
what you need is a little function to loop through whatever array/object is set as the dataProvider for your combobox. and search for data that matches n:
ActionScript Code:
// an array populated by an xml file. (populated by me for this example) var dataArray = [     { label:"Harry",   data:123456 },     { label:"Sally",   data:123457 },     { label:"Fred",    data:123458 },     { label:"Roxanne", data:123459 } ]; // comboBox.dataProvider = dataArray; // // the data we want to search for. var userDefinedVariable = 123459; // // set the selectedIndex to whatever the queryData function returns. comboBox.selectedIndex = queryData( userDefinedVariable ); // function queryData( query ) {     trace( "searching for:" + query + newline );     //     // loop through the array that provides the data to the combobox.     var l = dataArray.length;     for ( var i=0; i<l; i++ )     {         var trc = "[" + i + "] : " + dataArray[i].data;         // test for equality between the query parameter passed and the value of data for the current item.         // if the two match, return the value of i. which, in this example, will equal 3 which is the index of 123459.         if ( dataArray[i].data == query ) return i;         else trc += " > item not found.";         //         trace( trc );     }     // if the query value isn't found, default to something else ( in this case the first item).     return 0; }

---
i should mention that the method i suggested in my earlier post is the component v1 way. if you're using v2 it would be:
ActionScript Code:
instanceNameOfComboBox.selectedIndex = 2;
all code in my example up top is the v2 method.

Last edited by flashead; 01-19-2006 at 04:35 AM.
flashead is offline   Reply With Quote
Old 01-19-2006, 02:37 PM   #5
msimmons
Senior Member
 
msimmons's Avatar
 
Join Date: Jun 2005
Posts: 153
Default

Cool, thanks for that since we're in a time crunch we just redesigned the way we have the program to function so I won't be needing it this time but being such an obvious feature (one of many that flash makes difficult to do) I'm sure I'll need it again before too long so I've marked this post in favorites and will be sure to let you know how it works out.
Thanks again!
(oh and as far as v1 vs v2 go, I assume we're using v2 (we're using the latest version of flash)... I don't do any of the design, just the code behind it so not sure
M
msimmons is offline   Reply With Quote
Old 03-22-2006, 03:05 PM   #6
msimmons
Senior Member
 
msimmons's Avatar
 
Join Date: Jun 2005
Posts: 153
Default

Its been just a little over two months, seems like so long ago
Here is what I did, It is pretty much what you are saying, but in a reuseable function:
ActionScript Code:
//**FUNCTION FOR SELECTING CBO INDEX BASED ON DATA VALUE function selectCboIndexByData(theCbo,theDataValue):Number{     //loop through ever item in the cbo     for(var i:Number=0; i<theCbo.length; i++){         //check to see if we have found what we are looking for         if(theCbo.getItemAt(i).data == theDataValue){             //when we find what we want pass it back to the caller             return i;         }     }     //if we make it all the way to the end without finding what we want just return 0     return 0; }
now if I want to display something in any of my cbo's I just do this:
ActionScript Code:
myCbo.selectedIndex = selectCboIndexByData(myCbo, '612617');
msimmons is offline   Reply With Quote
Old 04-24-2008, 06:35 AM   #7
shauns1
Registered User
 
Join Date: Apr 2008
Posts: 1
Thumbs up Looping through items in Flash ComboBox

Quote:
Originally Posted by msimmons View Post
Its been just a little over two months, seems like so long ago
Here is what I did, It is pretty much what you are saying, but in a reuseable function:
ActionScript Code:
//**FUNCTION FOR SELECTING CBO INDEX BASED ON DATA VALUE function selectCboIndexByData(theCbo,theDataValue):Number{     //loop through ever item in the cbo     for(var i:Number=0; i<theCbo.length; i++){         //check to see if we have found what we are looking for         if(theCbo.getItemAt(i).data == theDataValue){             //when we find what we want pass it back to the caller             return i;         }     }     //if we make it all the way to the end without finding what we want just return 0     return 0; }
now if I want to display something in any of my cbo's I just do this:
ActionScript Code:
myCbo.selectedIndex = selectCboIndexByData(myCbo, '612617');

Just wanted to say thanks for this handy function.
shauns1 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting item in a List or Tree component- without clicking timtoady Components 0 09-14-2005 11:14 PM
Highlighting item in a Combo box clem_c_rock Components 1 07-29-2005 10:28 AM
Selecting a listbox item in script zacharyzblewski Components 5 12-09-2004 09:55 PM
dynamically add item to combo box component in flash mx 2004 rubalaser Components 6 06-06-2004 08:20 AM
Combo box selecting from script Drakon Components 3 04-05-2004 10:58 AM


All times are GMT. The time now is 03:34 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.