Here's an updated version of that code that allows you
to set the maximum value during runtime and
automatically compensates if the current total is
greater that the maximum allowable total set. Also,
this compensates for instances that a number bigger
than than the maximum allowable total is entered into
one of the 5 steppers:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application creationComplete="init()"
xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute">
<mx:Script>
        <![CDATA[
                import mx.events.NumericStepperEvent;
                
                [Bindable] private var total:uint = 0;
                                
                //set max value to maximum value of uint; change
value as required
                [Bindable] private var maxvalue:uint =
uint.MAX_VALUE;
                
                //determines remaining increments available,
initially equal to max value
                private var remaining:uint = uint.MAX_VALUE;
                
                
                private function init():void{
                        
                        n0.maximum = maxvalue;
                        n0.value  = maxvalue;
                        
                        n1.maximum = maxvalue;
                        n2.maximum = maxvalue;
                        n3.maximum = maxvalue;
                        n4.maximum = maxvalue;
                        n5.maximum = maxvalue;
                        
                }
                
                private function
updateValues(e:NumericStepperEvent):void{
                        if (e.target == n0){
                                maxvalue = n0.value;
                        }
                                                
                        total = n1.value + n2.value + n3.value + n4.value +
n5.value;
                        
                        if (total>maxvalue){ 
                                resetAddValues();
                                updateValues(e);
                                
                        }
                        
                        remaining = maxvalue - total;
                                                
                        updateMaxValues();
                        
                }
                
                private function updateMaxValues():void{
                        n1.maximum = remaining + n1.value;
                        n2.maximum = remaining + n2.value;
                        n3.maximum = remaining + n3.value;
                        n4.maximum = remaining + n4.value;
                        n5.maximum = remaining + n5.value;
                }
                
                private function resetAddValues():void{
                        n1.value = 0;
                        n2.value = 0;
                        n3.value = 0;
                        n4.value = 0;
                        n5.value = 0;
                }
        
        ]]>
</mx:Script>
        <mx:VBox>
                <mx:Label text="Maximum Total"/>
                <mx:NumericStepper id="n0"
change="updateValues(event)"/>
                <mx:HRule/>                     
                <mx:Label text="Stepper 1"/>
                <mx:NumericStepper id="n1" 
change="updateValues(event)"/>
                <mx:Label text="Stepper 2"/>
                <mx:NumericStepper id="n2" 
change="updateValues(event)"/>
                <mx:Label text="Stepper 3"/>
                <mx:NumericStepper id="n3" 
change="updateValues(event)"/>
                <mx:Label text="Stepper 4"/>
                <mx:NumericStepper id="n4" 
change="updateValues(event)"/>
                <mx:Label text="Stepper 5"/>
                <mx:NumericStepper id="n5" 
change="updateValues(event)"/>
                <mx:HRule/>             
                <mx:Label text="Total"/>
                <mx:NumericStepper id="n6"  maximum="{maxvalue}"
value="{total}" mouseChildren="false"/>
        </mx:VBox>
</mx:Application>

It just happend that we came across the same
requirement in a project last year, so i thought i'd
share.  Im also posting the repeater version which
gives the same result below should it be able to help
you or someone else in this group. Sorry for the
lengthy post. Cheers!

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute">
<mx:Script>
        <![CDATA[
                import mx.binding.utils.BindingUtils;
                import mx.events.NumericStepperEvent;
                import mx.controls.NumericStepper;
                import mx.events.FlexEvent;
                
                [Bindable] public var total:uint = 0;
                                
                //set max value to maximum value of uint; change
value as required
                [Bindable] public var maxvalue:uint =
uint.MAX_VALUE;
                
                //determines remaining increments available,
initially equal to max value
                private var remaining:uint = uint.MAX_VALUE;
                
                [Bindable]
         private var dp:Array = [0, 1, 2, 3, 4, 5];   


                
                private function
updateValues(e:NumericStepperEvent):void{
                        var stepper:NumericStepper = (e.target as
NumericStepper);
                        
                        if (stepper.name !="n"+(dp.length-1).toString()){
                                if (stepper.name == "n0"){
                                        
                                        maxvalue = stepper.value;
                                
                                }
                                                        
                                total = getTotal();
                                
                                if (total>maxvalue){ 
                                        resetAddValues();
                                        updateValues(e);
                                        
                                }
                                
                                remaining = maxvalue - total;
                        
                                                        
                                updateMaxValues();
                                
                        }
                }
                
                private function getTotal():uint{
                        var temptotal:uint = 0;
                        for (var i:uint = 0; i<step.length; i++){
                                var o:NumericStepper = step[i];
                                if (o.name !="n0" &&
o.name!="n"+(dp.length-1).toString() ){                         
                                        temptotal+=o.value;
                                }
                        }
                        return temptotal;
                }
                
                private function updateMaxValues():void{
                        for (var i:uint = 0; i<step.length; i++){
                                var o:NumericStepper = step[i];
                                if (o.name !="n0" &&
o.name!="n"+(dp.length-1).toString() ){                         
                                        o.maximum = remaining + o.value;
                                
                                }
                                
                        }
                        
                }
                
                private function resetAddValues():void{
                        for (var i:uint = 0; i<step.length; i++){
                                var o:NumericStepper = step[i];
                                if (o.name !="n0" &&
o.name!="n"+(dp.length-1).toString() ){                         
                                        o.value = 0;
                                        
                                }
                        }
                } 
                
                private function initializeChild(e:FlexEvent):void{
                
                        var o:NumericStepper = e.currentTarget as
NumericStepper;
                                                
                        o.name = "n"+o.getRepeaterItem().toString();
                        
                        if(o.getRepeaterItem()<dp.length-1){
                                
                                o.maximum = maxvalue;
                                
                                if (o.getRepeaterItem()==0){
                                        
                                        o.value = maxvalue;
                                }
                        }else{
                                if (o.name=="n"+(dp.length-1).toString()){
                                        o.mouseChildren = false;
                                                
                                
BindingUtils.bindProperty(o,"maximum",this,"maxvalue");
                                        
                                
BindingUtils.bindProperty(o,"value",this,"total");
                                }
                        }
                                
                        
                }
                
        
        ]]>
</mx:Script>
        <mx:VBox>
                <mx:Repeater id="n" dataProvider="{dp}"
count="{dp.length}">
                        <mx:NumericStepper id="step"
change="updateValues(event)"
creationComplete="initializeChild(event)"/> 
                </mx:Repeater>          
        </mx:VBox>      
</mx:Application>



      
____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
http://tools.search.yahoo.com/newsearch/category.php?category=shopping

Reply via email to