Using Event handler or something similar - button

I'm working on Adobe Flash CS5 and on actionscript 3 - My question is how can I make a button that only becomes available to click on once the four other buttons have been clicked and the scenes have been accessed?

Let's assume that your 5 buttons are always available on the timeline and they are at the top document level (if not, we need to make some adjustments). Let's also assume that, from your question, you're not ready for the best practice of a document Class file, so this will be written as timeline script.
//assumes the 4 buttons are on the stage, named button1, button2, etc.
var prerequisiteButtons:Array = [button1, button2, button3, button4];
var prerequisitesClicked:Array /*of Boolean*/ = [];
coyButton.enabled = false;//your fifth button, who plays hard to get
//loop through and listen for clicks on your prerequisite buttons
for each (var button:DisplayObject in buttons) {
button.addEventListener(MouseEvent.CLICK, checkAllClicked);
}
//check to see if all the buttons have been clicked
function checkAllClicked(e:Event):void {
//find the button in the prerequisite buttons array
var buttonIndex:int = prerequisiteButtons.indexOf(e.currentTarget);
//set the matching index to true in the array that keeps track of what has been clicked
prerequisitesClicked[buttonIndex] = true;
//count how many of them have been clicked
var prerequisiteDoneCount:int = 0;
for (var i:int = 0; i< prerequisitesClicked.length; i++) {
if (prerequisitesClicked[i]) prerequisiteDoneCount++;
}
//if all the buttons have been clicked, enable the button (may also want to add a listener here)
if (prerequisiteDoneCount==prerequisiteButtons.length) coyButton.enabled = true;
}

Related

dojo enhanced datagrid loses focus after edit

I have an enhanced datagrid that uses a jsonreststore.
I have set the editable cell to auto-save. To do this, I added a onApplyCellEdit property. after successful save (by pressing enter key) the server responds with the new data and my datagrid is updated. however, the focus on the cell just edited loses focus. As a result I cannot use the arrow key to navigate to the next cell, forcing me to use the mouse. Is there any way to regain focus so that I can use the keyboard to navigate after editing?
I have done following code to kind of regain the focus using setTimeout but I think it is not a good solution because if it takes too long for the server to respond the cell will regain focus first and when the response comes in and cell is updated, the cell will lose focus:
var accounts = new EnhancedGrid({
structure: account_layout,
selectionMode: "single",
canSort: function(){
return false
},
onApplyCellEdit: function(inValue,inRowIndex,inFieldIndex){
if(this.store){
this.store.save();
window.setTimeout(function(){
var cells = accounts.layout.cells;
var fieldLocation;
for(var i = 0; i < cells.length; i++){
if(inFieldIndex === cells[i].field){
fieldLocation = i;
break;
}
}
accounts.focus.setFocusCell(cells[fieldLocation],inRowIndex);
},500);
}
}
});
I also tried:
this.store.save().then(function(){ ...});
But it's not working.
EDIT
To clarify question, I added sentence after first sentence of 2nd paragraph.

How to get the reference to a newly created Button with Actionscript?

I have a MXML button:
<mx:Button id="myButton1"/>
How do I create N number of Buttons with Actionscript: myButton2, myButton3, myButton4... myButtonN ?
And how to get the reference to the newly created buttons right after they are created? Like
I should be able to do myButtonN.x = 100 right after it's created.
This is pretty basic stuff...you might want to start with some Flex tutorials, or read through one of the many excellent books.
Here's a blob of code for you to copy & paste and see how it works for you:
private var buttons:Array = [];
public function createButtons():void {
for(var i:int=0; i<100; i++) {
buttons[i] = new Button();
buttons[i].label = "Button "+i;
buttons[i].x = i * 50;
addChild(buttons[i]); // NOTE: use addElement instead of addChild in Flex 4
}
}
It's not tested, so might need some minor typos, but you should be able to get the idea.

Flex Problem Enabling and Disabling Button in List

I have a list with a dataprovider, it lists out buttons encapsulated in an item renderer. All I want it to do is have a skin that it changes to when it is clicked. I can get this to happen, but then it just goes back to its up state. I want it to stick on the down state, which I have to do by disabling the button.
So I tried this:
buttonList.selectedItem.enabled = false;
for(var i:Number = 0; i< buttonList.numChildren; i++)
{
var loopBtn = buttonList.getChildAt(i);
if(loopBtn != buttonList.selectedItem)
{
loopBtn.enabled = true;
}
}
But this doesn't seem to work. What am I doing wrong here?
Maybe you want to use a toggle button here?
<mx:Button toggle="true" ... />
At least it would stay in the down state after being pressed.
You need a static variable 'selectedButton' in the class those buttons that retains the latest selected button.
on click you set the selectedButton back to non-selected before selecting the new one.
HTH

How do i know that which button is clicked in flex?

for (iss = 0; iss < listOfProductIds2.length; iss++)
{
// Alert.show(listOfProductIds2[iss]);
var productMain:VBox=new VBox();
var p1:HBox=new HBox();
var l1:Label=new Label();
var b1:Button=new Button();
var spacer:Spacer=new Spacer();
spacer.width=300;
b1.label="Remove";
b1.setConstraintValue("id","");
b1.addEventListener(MouseEvent.CLICK,removeProduct);
l1.text="Product "+iss;
p1.setActualSize(500,500);
p1.addChild(l1);
p1.addChild(spacer);
p1.addChild(b1);
productMain.addChild(p1);
}
function removeProduct(event:MouseEvent):void
{
// How do i know which button is clicked
}
Use event.currentTarget (instead of event.target) because event.target might be the Label component or some styling component within the button, but currentTarget is assured to be the object with which the listener was registered.
To get a handle to the button that was clicked you can just cast the currentTarget to a button.
function removeProduct(event:MouseEvent):void
{
var b1:Button = Button(event.currentTarget);
}
The method setConstraintValue is for setting layout constraints, not setting id. The id property is used by mxml for creating variable names for objects. You can get/set id as you would get/set any other property (say width) - but neither have I seen anyone doing that nor do I see any need to do that in the first place.
event.target should point to the Button you clicked on, shouldn't it ? However you should probably give ids to the buttons to be able to differenciate them (since you create them dynamically.)
Look at event.target.
If ids are assigned dynamically as in the example given b1.id = "button_" + listOfProductIds2[iss]
Then the function that processes the click event would look at the currenttarget, and what I usually do is do a string replace on the part of the id that you know is not dynamic like "button_" with "", which leaves you with the name of the product.

How do I make an item editor in a flex list component commit it's changes?

I have a list component and I have an item editor for the items in the list. I would like to have a button that the user clicks once they are done with their changes because I am having them edit multiple pieces of data in the editor and I would also like to validate the data before closing the editor as well. I just don't know what to do on the button's click event to make the item editor close and commit it's changes to the data provider.
You'll want to use a validator to validate the data, and I think maybe do something with the updateComplete and change events to delay the updating of the list component:
http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=celleditor_073_17.html
I would use data binding and let Flex do the work for you.
Have an object myObject with a bindable property myList:IList. Bind the display to this object.
When you start editing, copy that list.
On MouseEvent.CLICK:
var ed:Editor // Your list editing object.
var edProvider:IList = ed.dataProvider;
var targList:IList = myObject.myList;
var bool:Boolean = ( myObject.myList.length > edProvider.length );
var len:int = ( bool )? targList.length: edProvider.length;
var item:* = null;
for( var i:int = 0; i < len; i++ )
{
try // a "just in case". You probably will never have a problem.
{
item = edProvider.getItemAt( i );
targList.setItemAt( item, i );
}
catch( error:Error )
{
continue;
}
}
To handle the editing of multiple fields in a List control, you will need to catch the ItemEditEnd event and then manually change the fields you are interested in.
See "Example: Using a custom item editor with a List control" in here - http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_9.html#226555.
Usually the List will handle the dispatching of this event for you when you focus out of a cell. I'm not sure of its properties off the top of my head, but you should be able to construct this event in your button click handler, and then just dispatch it yourself.

Resources