--CbRegion.as--
package com.flexsolutions.chapter1
{
import mx.controls.ComboBox;
public class CbRegion extends ComboBox
{
private var lazio:Array=new Array["Roma","Frosinone",
"Rieti", "Latina", "Viterbo"];
private var tuscany:Array=new Array("Arezzo", "Firenze",
"Grosseto" , "Livorno" , "Lucca","Massa-Carrara" ,"Pisa" , "Pistoia", "Prato", "Siena");;
}
private var allRegions:Array=lazio.concat(tuscany);
private var region:String = "all";
[Inspectable(defaultValue="all",enumeration=lazio,tuscany,all)]
//You use the <mx:Metadata> tag to insert metadata tags in an MXML file.
//Metadata tags provide information to the Flex compiler that describes how your MXML components are used in a Flex application.
// Metadata tags do not get compiled into executable code, but provide information to control how portions of your code get compiled
public function set chooseRegion(regionParam:String):void{
region=regionParam;
if(region="lazio"){
this.dataProvider = lazio;
}else if(region="tuscany"){
this.dataProvider = tuscany;
}else{
this.dataProvider = allRegions;
}
}
public function get chooseRegion(){
return region;
}
//Getter and setter methods are accessor methods that enable you to change private
//class properties and make them read-only and write-only. These methods can be
//invoked outside the class and give you the ability to create properties with sophisti-
//cated functionality that you can access like simple properties.
public function CbRegion()
{
}
}
--Test.mxml--
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:comp="com.flexsolutions.chapter1.*">
<!--The file will contain only the native namespace mx. However, to be able to access
the ActionScript class, you have to first create a custom namespace on the
Application tag xmlns:comp="com.flexsolutions.chapter1.*"-->
<mx:Panel x="101" y="79" width="250" height="200" layout="absolute">
<comp:CbRegion id="mycom" chooseRegion="lazio" x="35" y="87"/>
</mx:Panel>
<mx:Text x="374.5" y="123" text="1111" width="111" height="40"/>
</mx:Application>
--advance Program--
Des:add radio buttons to permit the user to select the data provider to use on the ComboBox:
<mx:RadioButtonGroup id="regionType" itemClick="mycom.changeRegion(event.currentTarget.selecetedValue)"/>
<mx:RadioButton x="10" y="56" label="lazio" groupName="regionType" />
<mx:RadioButton x="10" y="82" label="tuscany" groupName="regionType" />
<mx:Text x="10" y="132" text="Text is{mycom.chooseRegion()}" width="139"/>
<!--CbRegion.as-->
[Bindable(event="changeRegionEvt")]
//This event is registered by the metadata [Bindable], which is declared on the getter functions chooseRegion() and changeRegion():
public function changeRegion(regionParam:String){
region=regionParam;
if(region=="lazion"){
this.dataProvider=lazion;
}else if(region=="tuscany"){
this.dataProvider = tuscany;
}else{
this.dataProvider = allRegions;
}
dispatchEvent(new Event("changeRegionEvt"));
}