AS3 ViewStack content clipping - actionscript-3

I have the next code (Flash Builder 4.5)
This main appliction file, for testing me viewstack component.
TabPanelTest
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function creationCompleteHandler(event:FlexEvent):void
{
tp.AddPanel("111", new Button());
tp.AddPanel("444", new TestPanel());
tp.AddPanel("2222222", new Button());
tp.AddPanel("3333333333", new Button());
}
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
tp.RemoveAllPanels();
}
]]>
</fx:Script>
<ns1:TabPanel id="tp" x="25" y="27" width="321" height="199">
</ns1:TabPanel>
<s:Button x="439" y="25" label="Кнопка1" click="button1_clickHandler(event)"/>
</s:Application>
This is TabPanel, that have public methods AddPanel and RemoveAllPanels.
TabPanel
protected function creationCompleteHandler(event:FlexEvent):void
{
//buttons1.dataProvider=new ArrayCollection();
}
public function AddPanel(name:String, element:IVisualElement):void{
var panel:NavigatorContent=new NavigatorContent();
panel.label=name;
panel.addElement(element);
panels.addElement(panel);
}
public function RemoveAllPanels():void{
//panels.swapElements(swapElementsAt(0,2);
panels.removeAllElements();
/*var but:Button;
but=new Button();
but.label=name;
buttons.addElement(but); */
}
]]>
</fx:Script>
<mx:ViewStack id="panels" x="0" y="31" width="100%" height="100%" borderColor="#000000"
borderStyle="solid" borderVisible="true" clipContent="true" includeInLayout="true">
</mx:ViewStack>
<s:ButtonBar id="buttons1" x="0" y="0" width="100%" dataProvider="{panels}"
justificationStyle="pushOutOnly"/>
</s:Group>
This is testing panel, that have size more than viewstack, and it must clipping.
TestPanel
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%" height="100%">
<fx:Script>
<![CDATA[
import spark.components.NavigatorContent;
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
NavigatorContent(this.parent.parent.parent).label="bebe";
}
]]>
</fx:Script>
<fx:Declarations>
</fx:Declarations>
<s:Button x="163" y="35" width="315" height="209" label="Кнопка"
click="button1_clickHandler(event)"/>
</s:Group>
When i click to "444" in buuton bar my content don't clipping. What's problem?

Problem is in this line with TestPanel custom component: -
<s:Button x="163" y="35" width="315" height="209" label="Кнопка"
click="button1_clickHandler(event)"/>
If you are adding any component to parent container: -
child component will consider x and y position with respect to parent.
If you want to fit your child component you should use x=0 and y=0
as well as width = 100% and height = 100%
Try this and see out put: -
<s:Button x="163" y="35" width="100%" height="100%" label="Кнопка"
click="button1_clickHandler(event)"/>
Again Try this and see out put: -
<s:Button x="0" y="0" width="100%" height="100%" label="Кнопка"
click="button1_clickHandler(event)"/>
You will come to know how component behavior.
Hope this may solve your problem.....

Related

Flex - How to do calculation in Flash Builder

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">
<fx:Script>
<![CDATA[
protected function calculate_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:actionContent>
<s:Button label="Back" click="navigator.pushView(MainHomeView)" styleName="back"/>
</s:actionContent>
<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="mywieght" x="216" y="40" width="228" prompt="0.0kg" textAlign="right"/>
<s:TextInput id="myheight" x="216" y="119" width="228" prompt="0.0cm" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
fontStyle="italic"/>
<s:Label id="myresult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
verticalCenter="99"/>
</s:View>
this is the gui for a BMI calculator. i dont really have the basic of using flash builder. can anyone teach me how to use the data input by user inside the textinput and then use it for calculation and display it? thanks
First you assign a click handler to your button so your calculate_clickHandler method will be called on button click:
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36" fontStyle="italic" click="calculate_clickHandler(event)"/>
Then you do your calculations and pass your result to the myresult Label:
protected function calculate_clickHandler(event:MouseEvent):void
{
var height:Number = Number(myheight.text);
var weight:Number = Number(mywieght.text); // you have a typo here, wiegth instead of weight ;)
myresult.text = height * weight;
}
And you probably want a popView() on back button click. This will remove the current view and go back to your last view. push() will add another view:
<s:Button label="Back" click="navigator.popView()" styleName="back"/>
Try this:
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
protected function calculate_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
const myWeightInKG:Number = Number(myWeight.text);
const myHeightInMeter:Number = Number(myHeight.text) * 0.01;
const BMI:Number = myWeightInKG / (myHeightInMeter * myHeightInMeter);
const BMI_withOneDecimalPlace:Number = int(BMI * 10)/10;
myResult.text = BMI_withOneDecimalPlace.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="myWeight" x="216" y="40" width="228" prompt="Weight in Kg(e.g. 56)" textAlign="right"/>
<s:TextInput id="myHeight" x="216" y="119" width="228" prompt="Weight in cm(e.g. 157)" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
fontStyle="italic" click="calculate_clickHandler(event)"/>
<s:Label id="myResult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
verticalCenter="99"/>
</s:Application>

Borders of AdvancedDataGrid disappears in popup

The borders of AdvancedDataGrid are disappearing. I am using the same instance of popup object to show at different times.
For the first time the popup and ADG borders are fine but when i launch the popup next time, the borders get disappeared. Though borders reappear if i try to adjust column size. I am not able to get the cause of this issue and therefore not able to rectify it either.
I tried to readjust visibility of ADG and also tried to call invalidateDisplayList(), but no success.
*ADG = AdvancedDataGrid
I am attaching code snippets.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="onCC()" width="1500" height="1000">
<s:layout>
<s:VerticalLayout />
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.advancedDataGridClasses.AdvancedDataGridSortItemRenderer;
import mx.managers.PopUpManager;
[Bindable]private var dp:ArrayCollection = new ArrayCollection();
private function onCC():void
{
var obj1:Object = new Object();
obj1.col1 = "col11";
obj1.col2 = "col12";
obj1.col3 = "col13";
dp.addItem(obj1);
obj1 = new Object()
obj1.col1 = "col21";
obj1.col2 = "col22";
obj1.col3 = "col23";
dp.addItem(obj1);
}
var popup:PopupPanel = new PopupPanel();
private function launchPopup():void
{
PopUpManager.addPopUp(popup, this as DisplayObject);
PopUpManager.centerPopUp(popup);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:AdvancedDataGrid sortableColumns="false" sortItemRenderer="{null}" dataProvider="{dp}" draggableColumns="false" >
<mx:groupedColumns>
<mx:AdvancedDataGridColumn headerText="PM1" dataField="col1" />
<mx:AdvancedDataGridColumnGroup headerText="PM2" >
<mx:AdvancedDataGridColumn headerText="NE" dataField="col2" />
<mx:AdvancedDataGridColumn headerText="FE" dataField="col3"/>
</mx:AdvancedDataGridColumnGroup>
</mx:groupedColumns>
</mx:AdvancedDataGrid>
<mx:Button label="launch popup" click="launchPopup()" />
</s:WindowedApplication>
<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="1000" height="750"
close="titlewindow1_closeHandler(event)">
<s:layout>
<s:HorizontalLayout paddingLeft="20" paddingRight="20" paddingTop="20" paddingBottom="20"/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;
[Bindable] private var dp:ArrayCollection = new ArrayCollection();
protected function titlewindow1_closeHandler(event:CloseEvent):void
{
// TODO Auto-generated method stub
PopUpManager.removePopUp(this);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:AdvancedDataGrid id="dg1" sortableColumns="false" sortItemRenderer="{null}" dataProvider="{dp}" draggableColumns="false" width="100%" height="100%">
<mx:groupedColumns>
<mx:AdvancedDataGridColumn headerText="PM1" dataField="col1" />
<mx:AdvancedDataGridColumnGroup headerText="PM2" >
<mx:AdvancedDataGridColumn headerText="NE" dataField="col2" />
<mx:AdvancedDataGridColumn headerText="FE" dataField="col3"/>
</mx:AdvancedDataGridColumnGroup>
</mx:groupedColumns>
</mx:AdvancedDataGrid>
<mx:AdvancedDataGrid id="dg2" sortableColumns="false" sortItemRenderer="{null}" dataProvider="{dp}" draggableColumns="false" width="100%" height="100%">
<mx:groupedColumns>
<mx:AdvancedDataGridColumn headerText="PM1" dataField="col1" />
<mx:AdvancedDataGridColumnGroup headerText="PM2" >
<mx:AdvancedDataGridColumn headerText="NE" dataField="col2" />
<mx:AdvancedDataGridColumn headerText="FE" dataField="col3"/>
</mx:AdvancedDataGridColumnGroup>
</mx:groupedColumns>
</mx:AdvancedDataGrid>
</s:TitleWindow>
I created a public method that the instance is non-null calls invalidateList() of the grid. Example:
// Update grids when the display is already instantiated
public function updates():void{
its grid.invalidateList();
}

Flex 4.6 : Make action for button in list

I'm beginner in Flex . my issue when to add button to list , i put the button in the itemrender but the action of navigator.pushView(views.Listde,ProblemsList.selectedItem);
make an error "Acess of undefined property navigator .
Code :
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import views.ButList;
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
navigator.pushView(views.Listde,ProblemsList.selectedItem);
}
]]>
</fx:Script>
<s:Group width="100%" height="100%" styleName="PCS.css">
<s:HGroup width="100%" height="100%" gap="2" verticalAlign="middle" horizontalAlign="center" paddingBottom="5" paddingLeft="5"
paddingRight="5" paddingTop="5">
<s:HGroup width="30%" height="100%" verticalAlign="middle" horizontalAlign="center">
<s:Label text="{data._AddedDate}" textAlign="left" verticalAlign="bottom" width="100%" height="100%"/>
</s:HGroup>
<s:VGroup width="50%" height="100%" verticalAlign="middle" horizontalAlign="right">
<s:BitmapImage source="images/{data.image}"/>
<s:TextArea editable="false" text="{data.description}"/>
<s:Label text="{data.price}"/>
<s:Button label="s" click="button1_clickHandler(event)"/>
</s:VGroup>
</s:HGroup>
</s:Group>
if any solution to resolve it by list or any component instead of list then i want button to navigate to view with the selected item and the change of the list to navigate to another view with the selected item also , then the change of list work success but the button action cannot defined with the navigator.
Thanks in advance for any help .
Create a custom event with data property and dispatch the event from item renderer or dispatch the list change event from the item renderer when clicking the button.
package myEvents
{
import flash.events.Event;
public class CustomEvent extends Event
{
public function CustomEvent(type:String,
data:Object=null) {
// Call the constructor of the superclass.
super(type);
// Set the new property.
this.data = data;
}
// Define static constant.
public static const MY_BUTTON_CLICKED:String = "myButtonClicked";
// Define a public variable to hold the state of the enable property.
public var data:Object;
// Override the inherited clone() method.
override public function clone():Event {
return new CustomEvent(type, data);
}
}
}
MyItemRenderer.mxml
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
import views.ButList;
protected function button1_clickHandler(event:MouseEvent):void
{
// dispatch a custom
(this.owner as List).dispatchEvent(new CustomEvent(CustomEvent.MY_BUTTON_CLICKED, data));
//navigator.pushView(views.Listde,ProblemsList.selectedItem);
}
]]>
</fx:Script>
<s:Group width="100%" height="100%" styleName="PCS.css">
<s:HGroup width="100%" height="100%" gap="2" verticalAlign="middle" horizontalAlign="center" paddingBottom="5" paddingLeft="5"
paddingRight="5" paddingTop="5">
<s:HGroup width="30%" height="100%" verticalAlign="middle" horizontalAlign="center">
<s:Label text="{data._AddedDate}" textAlign="left" verticalAlign="bottom" width="100%" height="100%"/>
</s:HGroup>
<s:VGroup width="50%" height="100%" verticalAlign="middle" horizontalAlign="right">
<s:BitmapImage source="images/{data.image}"/>
<s:TextArea editable="false" text="{data.description}"/>
<s:Label text="{data.price}"/>
<s:Button label="s" click="button1_clickHandler(event)"/>
</s:VGroup>
</s:HGroup>
</s:Group>
Your view class
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="MY View">
<s:layout>
<s:VerticalLayout paddingTop="10"/>
</s:layout>
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
private function onListCreationComplete_Handler():void
{
myList.addEventListener(CustomEvent.MY_BUTTON_CLICKED, onItemRendererButtonClicked);
}
protected function onItemRendererButtonClicked(event:CustomEvent):void {
var data:Object = event.data;
navigator.pushView(views.Listde,data);//myList.selectedItem
}
]]>
</fx:Script>
<s:Label text="Select an view"/>
<s:List id="myList"
width="100%" height="100%"
labelField="firstName" itemRenderer="MyItemRenderer"
change="myList_changeHandler(event)" creationComplete="onListCreationComplete_Handler()">
<s:ArrayCollection>
<fx:Object description="Fruits" image="assets/icons/1.png" _AddedDate="15/05/14" price="154"/>
<fx:Object description="New Item" image="assets/icons/2.png" _AddedDate="15/05/14" price="154"/>
<fx:Object description="Doll" image="assets/icons/3.png" _AddedDate="15/05/14" price="154"/>
</s:ArrayCollection>
</s:List>
</s:View>
I hope this might help u. There are tons of examples out there for list and item renderer for mobile.
Based on your ItemRenderer, you can simply listen to the IndexChangeEvent in your View. In this case, you don't need a separate button at all - the whole ItemRenderer can act as the button.
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title="MY View">
<s:layout>
<s:VerticalLayout paddingTop="10"/>
</s:layout>
<fx:Script>
<![CDATA[
import spark.events.IndexChangeEvent;
import views.ButList;
protected function myList_changeHandler(event:IndexChangeEvent):void {
navigator.pushView(views.Listde,myList.selectedItem);
}
]]>
</fx:Script>
<s:Label text="Select an view"/>
<s:List id="myList"
width="100%" height="100%"
labelField="firstName" itemRenderer="MyItemRenderer"
change="myList_changeHandler(event)">
<s:ArrayCollection>
<fx:Object description="Fruits" image="assets/icons/1.png" _AddedDate="15/05/14" price="154"/>
<fx:Object description="New Item" image="assets/icons/2.png" _AddedDate="15/05/14" price="154"/>
<fx:Object description="Doll" image="assets/icons/3.png" _AddedDate="15/05/14" price="154"/>
</s:ArrayCollection>
</s:List>
</s:View>

Flex update XML

im new to flex development and in need of assistance.
I have a xml file that contains data that i would like to update if the user changes anything, the file is saved on his local program files where the application is.
Here is my code that reads the XML file:
<fx:Declarations>
<s:HTTPService id="systemData" url="data/system.xml" method="POST"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var systemInfo:Object = new Object();
private var interval:uint;
[Bindable]
private var xmlData:ArrayCollection = new ArrayCollection();
private function viewMain(event:Event):void
{
mainWindow.visible = true;
systemWindow.visible = false;
}
private function viewSystem(event:Event):void
{
mainWindow.visible = false;
systemWindow.visible = true;
}
public function init():void
{
systemData.addEventListener(ResultEvent.RESULT, getXML);
systemData.send();
interval = setInterval(reloadXML, 1800000);
}
private function getXML(e:ResultEvent):void
{
systemInfo = systemData.lastResult.system;
systemIP.text = systemInfo.systemip;
systemPort.text = systemInfo.systemport;
systemUsername.text = systemInfo.systemusername;
systemPassword.text = systemInfo.systempassword;
}
private function reloadXML():void
{
systemData.send();
}
]]>
</fx:Script>
<s:BorderContainer id="mainWindow" width="100%" height="100%" visible="false">
<s:Button x="434" y="102" label="System Settings" click="viewSystem(event)"/>
<s:Button x="468" y="147" label="Button" />
</s:BorderContainer>
<s:BorderContainer id="systemWindow" width="100%" height="100%" visible="true">
<s:Panel width="800" height="600" horizontalCenter="0" title="System Settings"
verticalCenter="0">
<s:Label x="33" y="41" text="Server IP:"/>
<s:Label x="33" y="66" text="Port:"/>
<s:Label x="33" y="90" text="Username:"/>
<s:Label x="33" y="115" text="Password:"/>
<s:TextInput x="182" y="31" width="250" id="systemIP"/>
<s:TextInput x="182" y="56" width="250" id="systemPort"/>
<s:TextInput x="182" y="80" width="250" id="systemUsername"/>
<s:TextInput x="182" y="105" width="250" id="systemPassword"/>
<s:Button x="345" y="135" label="Save to XML" />
<s:Button x="718" y="-26" label="Main" click="viewMain(event)"/>
</s:Panel>
</s:BorderContainer>
The XML File:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<system>
<systemip>127.0.0.0</systemip>
<systemport>80</systemport>
<systemusername>rootu</systemusername>
<systempassword>passu</systempassword>
</system>
Thanks in advance
To write the updated content back in your xml file, you need to write the file back. I assume your application is an Adobe AIR application. You can make use of "File" class to read and write the content.
Link: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html

How to come back in main application on click of logout button which is in application's component in flex4?

Please check the code and tell me How I will be back in Login Page after click of logout which is in application's component.
Project.mxml
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:StringValidator source="{uname}" id="unameValid" property="text"/>
<mx:StringValidator source="{password}" id="passwordValid" property="text"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
import mx.validators.Validator;
private var myValidators:Array = new Array;
private var nav:Navigation;
private function init():void{
btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
myValidators = [unameValid,passwordValid];
}
private function validateForm(event:Event):void{
//Alert.show("in validate form");
var errors:Array = Validator.validateAll(myValidators);
if(errors.length == 0){
loginUser();
}else{
Alert.show("in else");
}
}
private function loginUser():void{
//Alert.show("In login Form");
nav = new Navigation();
this.addElement(nav);
}
]]>
</fx:Script>
<s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110"
verticalAlign="middle" verticalCenter="-280"/>
<mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
<mx:FormItem label="UserName">
<s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
</mx:FormItem>
<mx:FormItem label="Password">
<s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
</mx:FormItem>
<mx:FormItem>
<mx:HBox horizontalGap="20">
<s:Button label="Login" id="btnLogin" />
<mx:LinkButton label="Register" id="register"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</s:Application>
Navigation.mxml
<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import spark.components.Application;
private function logout(event:MouseEvent):void{
}
]]>
</fx:Script>
<s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
<mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
<s:NavigatorContent label="DashBoard" width="100%" height="100%">
<s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
<s:Label text="in DashBoard"/>
</s:BorderContainer>
</s:NavigatorContent>
<s:NavigatorContent label="User Information" width="100%" height="100%">
<s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
<s:Label text="in UserInfo"/>
</s:BorderContainer>
</s:NavigatorContent>
</mx:ViewStack>
<s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>
You should create a custom event:
package
{
import flash.events.Event;
public class LogoutEvent extends Event
{
public static const LOG_OUT:String = "logOut";
public function LogoutEvent(type:String)
{
super(type,false,false);
}
public override function clone():Event
{
return new LogoutEvent(type);
}
public override function toString():String
{
return formatToString("LogoutEvent");
}
}
}
Now you should dispatch this event:
<mx:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" width="100%" height="100%" xmlns:local="*">
<fx:Metadata>
[Event(name="logOut", type="LogoutEvent")]
</fx:Metadata>
<fx:Script>
<![CDATA[
import spark.components.Application;
private function logout(event:MouseEvent):void{
dispatchEvent(new LogoutEvent(LogoutEvent.LOG_OUT));
}
]]>
</fx:Script>
<s:TabBar id="tabs" left="10" y="5" dataProvider="{vs}"/>
<mx:ViewStack id="vs" width="90%" height="90%" left="10" y="30">
<s:NavigatorContent label="DashBoard" width="100%" height="100%">
<s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
<s:Label text="in DashBoard"/>
</s:BorderContainer>
</s:NavigatorContent>
<s:NavigatorContent label="User Information" width="100%" height="100%">
<s:BorderContainer width="100%" height="100%" borderWeight="2" cornerRadius="3" dropShadowVisible="true">
<s:Label text="in UserInfo"/>
</s:BorderContainer>
</s:NavigatorContent>
</mx:ViewStack>
<s:Button x="494" y="1" label="Logout" id="btnLogout" click="logout(event);"/>
</mx:Panel>
Finally, you should handle it and close your window:
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:StringValidator source="{uname}" id="unameValid" property="text"/>
<mx:StringValidator source="{password}" id="passwordValid" property="text"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.ValidationResultEvent;
import mx.validators.Validator;
private var myValidators:Array = new Array;
private var nav:Navigation;
private function init():void{
btnLogin.addEventListener(MouseEvent.CLICK,validateForm);
myValidators = [unameValid,passwordValid];
}
private function validateForm(event:Event):void{
//Alert.show("in validate form");
var errors:Array = Validator.validateAll(myValidators);
if(errors.length == 0){
loginUser();
}else{
Alert.show("in else");
}
}
private function loginUser():void{
//Alert.show("In login Form");
nav = new Navigation();
this.addElement(nav);
nav.addEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
}
private function nav_logOutHandler(event:LogoutEvent):void
{
removeElement(nav);
nav.removeEventListener(LogoutEvent.LOG_OUT, nav_logOutHandler);
nav = null;
}
]]>
</fx:Script>
<s:Label text="Login Form" fontWeight="bold" fontSize="16" horizontalCenter="-110"
verticalAlign="middle" verticalCenter="-280"/>
<mx:Form horizontalCenter="-120" verticalCenter="-200" fontWeight="bold" verticalGap="20">
<mx:FormItem label="UserName">
<s:TextInput id="uname" restrict="A-Z a-z" focusIn="uname.errorString = null" text="Priyanka"/>
</mx:FormItem>
<mx:FormItem label="Password">
<s:TextInput id="password" displayAsPassword="true" focusIn="password.errorString = null" text="aamir#23"/>
</mx:FormItem>
<mx:FormItem>
<mx:HBox horizontalGap="20">
<s:Button label="Login" id="btnLogin" />
<mx:LinkButton label="Register" id="register"/>
</mx:HBox>
</mx:FormItem>
</mx:Form>
</s:Application>
The quickest way would be:
private function logout(event:MouseEvent):void{
parent.removeElement(this);
}
However, Constantiner's method is the proper way.
Also, if you are using flex 4, why not use the spark Form,FormItem,Panel and HGroup (Instead of HBox)