Sunday, February 8, 2009

Setting focus to a UI control

In Flex, if we validate a user entry, we would show an alert to the user and then set the focus to the incorrect field. Here, the setFocus() method should be called before the Alert.show() method is called.

Sunday, December 21, 2008

Global Variables in Flex Apps

Global variables are useful in numerous occasions in our application. So here it is how you do it in flex.

Here is how you do it..

Step 1: Import Application core package

       import mx.core.Application


Step 2:  Declare the variable in the main application (in our case it should be Indemand_Video.mxml)


  public var gVariable:String = "gVariable";


Step 3: Use the variable "gVariable" anywhere in the application as follows


Application.application.gVariable


I ran a test to access this global variable in chat.mxml. It worked fine.


I think we can use the same application object to access any public function in the Indemand_Video.mxml main application.. Is it strinking any bells ?


Update:


1) I created public alertTest():void function in indemand_video.mxml.

2) called the function in chat.mxml as Application.application.alertTest();

  Bingo it worked.....




Friday, November 28, 2008

AIR - Call a method of different application and pass variable

Some time we need to trigger(call) a function of different application(parent) while an event occurs in child application. Lets say, we need to select an item from available item list. To select an item, a new window is created and it lists available items. The user selects an item. Once the user selected an item, it should be reflected in parent window.

I have created a function called changeItem() in ParentWindow. Since the function is triggered by an event, it should have a variable of type Event.
The code of the changeItem() is

public function changeItem(event:Event,inItem:String):void
{

/* variable inItem is passed by the ChildWindow */
txtSelectedItem.text=inItem;
}


(To create and call a window please see multiple windows in AIR )

Pass the application as a parameter.
When creating the instance of the ChildWindow in the ParentWindow, declare a variable of type ParentWindow and initialize to "this" i.e current application. The code is given here
declare variable
private var thisInstance:ParentWindow;

calling the ChildWindow
/*newChildWindow is of type ChildWindow*/
newChildWindow=new ChildWindow();
/* intialize the variable thisInstance in init() function if you are referring the intansce in multiple places*/
thisInstance=this;
newChildWindow.parentWindowInstance=thisInstance;
newChildWindow.open();


In the ChildWindow (select item window), add a listener to the select button/component and call the function of the Parent window (changeItem).

add event listener function
cmd.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
{

/* parentWindowInstance variable is the instance of the ParentWindow and it is initialized by the ParentWindow when the instance of the ChildWindow is created
parentWindowInstance is declared in ChildWindow <Script> section */
parentWindowInstance.changeItem(e,pTxtInput.text);
}

here, a function is declared in the parameter place. This declaration acts as parameter passing trick.

Multiple window in AIR application

This piece of code gives a brief idea of how to work with multiple window. Invoking an application(mxml) from other applications(mxml).This piece is different from pop up window. Pop up window is a pre-defined class while creating a window application is user defined application.

In this example, I am calling ChildWindow.mxml from ParentWindow.mxml.

Call child Window
ParentWindow
in "<mx:Script>" section of the ParentWindow, create an instance of the ChildWindow, initialize and open the window.

Declare the intance of the child
private var newChild:ChildWindow;

initialize the instance
newChild= new ChildWindow();

to open the window
newChild.open();

ChildWindow
ChildWindow should be of Window component type ()

To pass variable from parent to child
declare the variable in the ChildWindow. When creating the instance, initialize the variable from the mainwindow. For example, to pass the value "yaazh" to the variable "name" of the child window, the code is given below

in ChildWindow
declare variable name in "<Script>"
section public var name:String

in ParentWindow when creating intance of the ChildWindow, assign the value to the variable of the ChildWindow instance.

newChild= new ChildWindow();
newChild.name="yaazh";
newChild.open();

this will work :-)

Tuesday, November 25, 2008

Get IPAddress into Flash

here is a php code and a flex example.

MXML CODE:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3. <mx:HTTPService id="dir" url="http://yourdomain.com/ip.php" resultFormat="e4x" />
  4.     <mx:Button  label="Get IP" click="dir.send()" x="258" y="238"/>
  5.     <mx:TextInput  text="Ip:{dir.lastResult.ip}" x="210" y="208"/>
  6. </mx:Application>

PHP CODE:

  1. <?php
  2.  $REMOTE_ADDR;
  3. if ($REMOTE_ADDR)
  4. {
  5.     $message"<result><ip>$REMOTE_ADDR</ip></result>";
  6. }
  7. else
  8. {
  9.     $message = "<result><ip>No Ip or any message.</ip></result>";
  10. }
  11. echo $message;
  12. ?>

Detecting Display Settings: Use the screenResolutionX and screenResolutionY properties of the system.capabilities object.

package{
import flash.display.Sprite;
import flash.system.Capabilities;
public class Main extends Sprite{
public function Main(){
trace(flash.system.Capabilities.screenResolutionX);
trace(flash.system.Capabilities.screenResolutionY);
}
}
}

प्रेम watch out, this might be userful to us in detecting the video resolution & laying out accordingly.

more useful samples like this can be found here - http://www.java2s.com/Code/Flash-Flex-ActionScript/CatalogFlash-Flex-ActionScript.htm

Monday, November 24, 2008

Amazon AWS - Remote Desktop (RDP) max users connected error

The AWS Windows server RDP allows concurrrent logins. But some times we have got error message like "max users connected" to server & it doesnt allow you to login.

Use this whenever this happens next time

mstsc /v:75.xxx.yyy.99 /admin
replace the ip for the corresponding server (ip address masked for security)

Thanks to Sandy boy for giving this tip