Tuesday, April 20, 2010

mx.controls.MenuBar

Recently I was working on a project where someone wanted the menu to open when went over. I thought how hard could this be there should be a hook in there to say open on mouse over but as usual I was wrong.
So I went digging and experimenting, it was interesting to see how many ways people have tried to solve this issue and talk about complicated. I am from the old school where the main design pattern is KISS (keep it simple stupid).
So I decide I could write one myself and hopefully make it less complicated, and surprisingly enough it worked out perfectly (well so far it has not been totally tested out yet).
Basically you extend mx.controls.MenuBar
In the constructor add two event listeners, set the buttomMode to true
Public function myMenuBar()
{
super;
this.buttonMode = true;
this.addEventListener(MouseEvent.MOUSE_OVER,mouseRollOverHandler);
}
The opening of the menu is done by the mouseRollOverHandler, which checks to see if the target is a MenuBarItem and if it is fires off a MouseEvent.MOUSE_DOWN, this will basically make it seem like the user clicked the menu bar.
private function mouseRollOverHandler(event:MouseEvent):void
{
     if (event.target is MenuBarItem)
     {
          event.target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
     }
}
I hope this was of some help.

Tuesday, April 13, 2010

Custom Phone Number Formatter

Well it has been a while since I have posted anything mainly because I have been busy at work trying to get a new version of our application completed and into production.


I am going to try and post a couple of items that I have had to deal with lately that caused me some issues. The first item I am going to look at is a custom phone number formatter, I build it out of a need to allowing the user to enter just about anything in there and have the system properly format the phone number with an extension.


I did the usual Google search and saw different pieces but never really liked any single one. So what I came up with is a custom phone number formatter that extends mx.formatters.PhoneFormatter. The format function basically checks to see the length of the phone number and performs the formatting based on the length.


If the length is 0 or less than 10, we place an error in the error variable and clear out the phone number


if( phoneNbr.length == 0 || phoneNbr.length < 10)


{


error="Invalid Length Phone Number";


phoneNbr = "";


}


If the length is 10 then we basically return the phone number that is formatted by the supper's format.


However if the length is greater than 10 we then strip off any spaces and ()-.x in the number then reassemble the phone number into a (###) ###-#### x#### format the x#### will basically pick up the remaining characters.


I hope this is of some help to everyone.