views:

481

answers:

3

Hi, I have a combo box in my application. I also have a variable called "Status". I want the combo box to be enabled only when the value of the Status variable is 5 or 6. Otherwise, I should not be able to change the value in the combo box. It should have the previous value only..

I had written a click event to disable the combo box. But even though the combo box is disabled, I get the drop down list of the combo box, and If I select some othe value in the drop down,it changes..Only after that it gets disabled..

How to avoid this? I want the drop down function itself to be disabled. This is the code I have written. Someone guide me.

<mx:FormItem label="Review Status:" width="100%" horizontalAlign="right">
  <mx:HBox>
     <mx:Label  width="30"/>
        <mx:ComboBox id="reviewStatus" dataProvider="{Status}" 
                     width="150" click="onStatusChange(event)"/> 
  </mx:HBox>

Action Script part:

private function onStatusChange(event:Event):void
{
  var i:int;
  for(i=0;i<defectDetails.length;i++)
  {
    var defStatusId:String=defectDetails.getItemAt(i).DefectStatusId;
    if(defStatusId=="5"){
               reviewStatus.enabled=true;
    }  
    else  if(defStatusId=="6"){
               reviewStatus.enabled=true;
    }
    else{
               reviewStatus.enabled=false;
             //reviewStatus.selectedItem.label="Review";
             reviewStatus.toolTip="Status can be changed only if Defect Status is Verified or Deferred.";

              //Alert.show("Status can be changed only if defect status is verified or deferred");
    }
   }
  }

If I use Change event also, for the first time the value is changed. Only after that,the combo box is disabled. How to retain the same value and disable the combo box when the status is not 5 or 6?

A: 

Call reviewStatus.close() before setting enabled to false.

Why are you doing it on the click event of the ComboBox? Better do it on the change event of defectDetails (or the control that uses defectDetails as its dataProvider).

Btw, there is another potential bug in there: You are setting the enabled value in a loop and not breaking the loop after setting it. The final value of reviewStatus.enabled would depend solely on the last item - defectDetails.getItemAt(defectDetails.length - 1).

Also, you're not setting the toolTip back to normal when enabled is true.

Amarghosh
A: 

Why the loop? Could you not just grab the selectedItem as Int on Status Change, i.e.:

reviewStatus.enabled = ((reviewStatus.selectedItem as Int)) == 5 || (reviewStatus.selectedItem as Int) == 6) ? false : true;

Though once it's been disabled, you probably won't be recieving any more StatusChange events, huh?

Shakedown
A: 

Maybe, you can use a binding function on 'enabled' for 'reviewStatus'. Like,

<mx:ComboBox id="reviewStatus" enabled = {checkEnabledForReviewStatus(defectStatusId)} />

And in the Script part,

private function checkEnabledForReviewStatus(defectStatusId:String):Boolean 
{
    return (defectStatusId == "5" || defectStatusId == "6") ? true: false;
}
Sai Prasad