Flex selectComboBox(), mkII

At the suggestion of a coworker, I've made a tiny change to my selectComboBox() function. The purpose of this function is to automatically select something in a ComboBox, such as if you're editing a form. But what if the function ends up doing nothing because no matches were found? Now it'll return false in that case. Also I properly capitalized it.

private function selectComboBox(cb:ComboBox, column:String, value:String):Boolean{
   if( value == null )
      return;
   
   for( var i:int=0; i<cb.dataProvider.length; i++ ){
      if( value == String(cb.dataProvider[i][column]) ){
         cb.selectedIndex = i;
         return true;
      }
   }
   return false;
}

Usage:

selectComboBox(cbType,"type",DiscFormData.Type);

It will search the dataprovider of cbType, looking at the "type" field, and select the item if it matches the third value passed in. Now to use the returned value...

isMatched = selectComboBox(cbType,"type",DiscFormData.Type);

if( !isMatched ){
   /*do some code... add this value to the dataprovider? error message?*/
}

Or hey, get real fancy...

if( !selectComboBox(cbType,"type",DiscFormData.Type) ){
   /*do some code... add this value to the dataprovider? error message?*/
}

Related Blog Entries

Comments
the code is messed up or you are using sme ninja code font.
# Posted By Scot | 6/4/08 6:11 PM
omg what is all that garbage? :(
# Posted By Murloc Oracle | 6/5/08 9:25 AM
NOTE TO SELF: Do not use "//" for a line comment in BlogCFC
# Posted By Murloc Oracle | 6/5/08 9:26 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner