02-15-2011 11:59 AM
Hi there I just started coding and while I'm using segmentedControl button, I doesn't know how to initiate a function when a segmentedcontrol option is pressed.
As you can see, I want it when the "newest" is pressed, it instantiates a function which does what I want, but how do i do that? I'm just a beginner and still learning how to code any help I greatly appreciated.
Solved! Go to Solution.
02-15-2011 12:12 PM
I sort of created the event listener for SegmentedControl using button,addeventlistener(mouseevent,function) sample, and here's my code
and apparently this one didn't worked...any advice?
02-15-2011 12:18 PM - edited 02-15-2011 12:20 PM
hey torres,
try using the Event.CHANGE instead and see what that yeilds. then check for the segemented control's selected index to see what button was pressed. on a side note for future reference, use the code option and copy and paste your code instead of taking screen shots of your code for faster help. images have to be approved by the moderators and can take time before we can see them publicly. good luck!
02-15-2011 12:19 PM
check the API doc, you should try listen Change Event instead of Mouse Event.
---------------------
a) If you like my response/post, please provide a Kudo (white star to the left).
b) If my post solved your problem please click on the Accept as Solution button.
02-15-2011 12:39 PM
here is a sample code i just wrote up that should be able to help you figure out the segmented control:
SegmentedControlTest.as:
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import qnx.ui.buttons.SegmentedControl;
import qnx.ui.data.DataProvider;
[SWF(width="1024", height="600", backgroundColor="#E8E8E8", frameRate="30")]
public class SegmentedControlTest extends Sprite
{
private var mySegment:SegmentedControl;
private var buttonArray:Array;
public function SegmentedControlTest()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
buttonArray = [{label: "Black"}, {label: "Red"}, {label: "Yellow"}];
mySegment = new SegmentedControl();
mySegment.dataProvider = new DataProvider(buttonArray);
mySegment.setPosition(150,125);
mySegment.setSize(300,30);
mySegment.selectedIndex = 0;
mySegment.addEventListener(Event.CHANGE, onChange);
addChild(mySegment);
}
private function onChange(e:Event):void
{
trace("A change has been made.");
switch (e.currentTarget.selectedIndex)
{
case 0:
fillBackground(0x000000);
break;
case 1:
fillBackground(0xFF0000);
break;
case 2:
fillBackground(0xFFFF00);
break;
}
}
private function fillBackground(value:uint):void
{
this.graphics.clear();
this.graphics.beginFill(value);
this.graphics.drawRect(0,0,1024,600);
this.graphics.endFill();
}
}
}
Make sure to use the e.currentTarget to get your segmented control and then you can access any of its properites. also notice that onces you use the .selectedIndex in the code when you create it, it will dispatch the Event.CHANGE event. so just be ready for that. good luck!
02-16-2011 12:11 AM
Awsome! thanks JRab for the sample! using switch and e.currentevent.selectedindex worked perfectly!