Example
Components/addressformclass.
Package Components { Import MX. Events. flexevent; Import MX. Controls. Button; Import MX. Controls. textinput; Import Flash. Events. mouseevent; Import MX. containers. form; Public Class Addressformclass Extends Form { // Components in the mxml must be // Declared public. This is a limitation in // The current version of flex and may change // In the future. Public VaR Submitbutton : Button; Public VaR Nameinput : Textinput; Public VaR Street : Textinput; Public VaR City : Textinput; Public VaR State : Textinput; Public VaR Country : Countrycombobox; // Constructor Public Function Addressformclass () : Void { Addeventlistener ( Flexevent. creation_complete, creationcompletehandler ) ; } // Creation complete is a good time to add Event Listeners and // Interact with child components. Private Function Creationcompletehandler ( Event : Flexevent ) : Void { Submitbutton. addeventlistener ( Mouseevent. Click, submithandler ) ; } // Gets called when the submit button is clicked Private Function Submithandler ( Event : Mouseevent ) : Void { // Gather the data for this form VaR Addressvo : Addressvo = New Addressvo () ; Addressvo. Name= Nameinput. Text; addressvo. Street = Street. Text; addressvo. City = City. Text; addressvo. State = State. Text; addressvo. Country = Country. selecteditem As String; VaR Submitevent : Addressformevent = New Addressformevent ( Addressformevent. Submit ) ; Submitevent. Data= Addressvo; // Dispatch an event to signal that the form has been submitted Dispatchevent ( Submitevent ) ; } } }
Components/addressform. mxml
<? XML version = "1.0" encoding = "UTF-8"?> <Custom: addressformclass Xmlns: MX =" Http://www.adobe.com/2006/mxml "Xmlns: Custom =" Components .* " > <Mx: formitem Label =" Name " > <Mx: textinput Id =" Nameinput " /> </MX: formitem> <Mx: formitem Label =" Street " > <Mx: textinput Id =" Street " /> </MX: formitem> <Mx: formitem Label =" City " > <Mx: textinput Id =" City " /> </MX: formitem> <Mx: formitem Label =" State/County " > <Mx: textinput Id =" State " /> </MX: formitem> <Mx: formitem Label =" Country " > <Custom: countrycombobox Id =" Country " /> </MX: formitem> <Mx: button Id =" Submitbutton "Label =" Submit " /> </Custom: addressformclass>
Components/addressformevent.
Package Components { Import Flash. Events. event; Import Components. addressvo; Public Class Addressformevent Extends Event { Public Static Const Submit : String = "Submit" ; Private VaR _ DATA: Addressvo; Public Function Addressformevent ( Eventname : String ) { Super ( Eventname ) ; } Public Function Set Data ( Value : Addressvo) : Void { _ DATA = Value; } Public Function Get Data () : Addressvo { Return _ Data; } } }
Components/addressvo.
Package Components { Public Class Addressvo { // We are using public properties for // Value object to keep this example short. In // Real-world application, make these properties // Private and use implicit accessors to expose them // So you can do validation, etc. If necessary. Public VaR Name : String; Public VaR Street : String; Public VaR City : String; Public VaR State : String; Public VaR Country : String; } }
Components/paddedpanel.
Package Components { Import MX. containers. Panel;Public Class Paddedpanel Extends Panel { Public Function Paddedpanel () { // Call the constructor of the superclass. Super () ; // Give the Panel a uniform 10-pixel // Padding on all four sides. Setstyle ( "Paddingleft" , 10) ; Setstyle ( "Paddingright" , 10 ) ; Setstyle ( "Paddingtop" , 10 ) ; Setstyle ( "Paddingbottom" , 10 ) ; } } }
Components/countrycombobox. mxml
<? XML version = "1.0" encoding = "UTF-8"?><Mx: ComboBoxXmlns: MX ="Http://www.adobe.com/2006/mxml"><Mx: dataprovider><Mx: String>United States</MX: String><Mx: String>United Kingdom</MX: String><! --Add all other countries.--></MX: dataprovider></MX: combobo</MX: ComboBox>
Package Components{ Import MX. Core. Application; Import MX. Events. flexevent; Import MX. Controls. Alert; Import Components. addressformevent; Import Components. addressvo; Import Flash. utils. describetype; Public Class Applicationclass Extends Application { // Components in mxml Public VaR Addressform : Addressform; Public Function Applicationclass () { Addeventlistener ( Flexevent. creation_complete, creationcompletehandler ) ; } // // Event handlers // Private Function Creationcompletehandler ( Event: Flexevent ) : Void { // The Custom addressform component dispatches a "Submit" // Event the form is submitted. Listen for this. Addressform. addeventlistener ( Addressformevent. Submit, submithandler ) ; } Private Function Submithandler ( Event : Addressformevent) : Void { // Get the value object (data) from the event object VaR Data : Addressvo = Event. Data As Addressvo; // Compose a string to display the contents of the value object to the user. VaR MSG : String = "You submitted the following information: \ r" ;// Use the New Introspection API and E4X to get a list of the Properties // In the data object and enumerate over them to compose the string. VaR Dataproperties : Xmllist = Describetype ( Data ) ... Variable; For Each ( VaR I : XML In Dataproperties ) { VaR Propertyname : String = I. @ name; msg + = I. @ name + ":" + Data [ I. @ name ] + "\ R" ; } // Display the contents of the address form to the user. Alert. Show ( MSG,"Thank you for submitting the form! " ) ; } } }
Main Application mxml File
xmlns: M = "
components. * "viewsourceurl ="
src/codebehind/index.html "width ="
400 "Height ="
310 "
>
Title = "
code behind "
>
id = "
addressform "
/>