ListBox Family Component
Entry นี้จะมาลองใช้ Component ถึง 3 อย่าง พร้อม ๆ กัน คือ
1.ComboBox - เลือกได้ item เดียว
2.List - เลือกหลาย item ก็ได้
3.Grid - เลือกหลาย item ก็ได้ แต่ว่า มันโชว์ในรูปแบบ ตารางได้
งั้นมาลองใช้กันเลย
1. ลาก component 3 อย่างที่จะใช้ไปใส่ไว้ที่ Library รวมทั้ง TextArea ด้วย (ไว้แสดงค่า)
2. ใส่ code ที่ frame แรกดังนี้
//สร้าง instance ของ TextArea และ กำหนด properties ต่างๆ
this.createClassObject(mx.controls.TextArea,"myTextArea",0);
myTextArea.move(20,20);
myTextArea.text = "Please select item in any components";
myTextArea.setSize(400,50);
myTextArea.wordWrap = true;
myTextArea.html = true;
//สร้าง instance ของ ComboBox และกำหนด properties ต่าง ๆ
this.createClassObject(mx.controls.ComboBox,"myComboBox",1);
myComboBox.move(20,80);
myComboBox.addItem("Mike","56");
myComboBox.addItem("Naomi","45");
myComboBox.addItem("Pugkung","80");
//สร้าง listener เพื่อรองรับ event ที่เกิดขึ้น กับ ComboBox
var cb_Listener:Object = new Object();
cb_Listener.change = function(evtObj:Object){
myTextArea.text = evtObj.target.selectedItem.label + " is "
+ evtObj.target.selectedItem.data + " kilograms";
}
myComboBox.addEventListener("change",cb_Listener);
//สร้าง instance ของ List และกำหนด properties ต่าง ๆ
this.createClassObject(mx.controls.List,"myList",2);
myList.move(20,120);
//add items to list and add thier datas
myList.addItem({label:"Mike",data1:"140",data2:"10"});
myList.addItem({label:"Naomi",data1:"160",data2:"18"});
myList.addItem({label:"Pugkung",data1:"178",data2:"24"});
//สร้าง listener เพื่อรองรับ event ที่เกิดขึ้น กับ List
var li_Listener:Object = new Object();
li_Listener.change = function(evtObj:Object){
var selectedObj:Object = evtObj.target.selectedItem;
myTextArea.text = selectedObj.label + " is "+ selectedObj.data2
+ "<br>and height is "+selectedObj.data1;
}
myList.addEventListener("change",li_Listener);
//สร้าง instance ของ DataGrid และกำหนด properties ต่าง ๆ
this.createClassObject(mx.controls.DataGrid,"myDataGrid",4);
myDataGrid.move(20,240);
myDataGrid.setSize(200,150);
//set column name เพื่อไว้ให้รู้ว่าแต่ละ column คืออะไร
myDataGrid.columnNames = ["Name","Sex","Country"];
//add item to datagrid
myDataGrid.addItem({Name:"Mike",Sex:"Male",Country:"USA",called:"man"});
myDataGrid.addItem({Name:"Naomi",Sex:"Female",Country:"Japan",called:"woman"});
myDataGrid.addItem({Name:"Pugkung",Sex:"Male",Country:"Korea",called:"man"});
//สร้าง listener เพื่อรองรับ event ที่เกิดขึ้น กับ DataGrid
var dg_Listener:Object = new Object();
dg_Listener.change = function(evtObj:Object){
var selectedObj:Object = evtObj.target.selectedItem;
myTextArea.text = selectedObj.Name + " is a " + selectedObj.called + " who lives in "
+ selectedObj.Country;
}
myDataGrid.addEventListener("change",dg_Listener);
จะเห็นว่า ในการใช้ datagrid นั้น ตอนที่ addItem ไป เราใส่ ค่าไป 4 ค่า แต่ ค่าที่แสดงผลบน datagrid นั้นมีแค่ 3 คือ ซึ่ง 3 ค่านี้มัน map กับ columnNames ที่ set ไว้ และ ส่วนอีกค่า(called) เราก็สามารถ กำหนดไว้ตอน addItem แล้วมาเรียกใช้ได้เช่นกัน
posted by flashas, at 2006-May-29 13:08:42
RadioButton Component
Radio button นั้น ต่างกับ checkbox ตรงที่ ใน 1 group มันจะเลือกได้แค่อันเดียว เพราะฉะนั้น flash เอง จึงมี property ที่มันเป็น group ให้อยู่แล้ว ไม่ต้องมานั่งทำเหมือน checkbox ใน entry ที่แล้ว
1.ลาก component TextArea, RadioButton และ Button ไปใส่ไว้ที่ Library
2.ใส่ code ที่ frame แรก ดังนี้
//สร้าง instance ของ TextArea
this.createClassObject(mx.controls.TextArea,"myTextArea",0);
//กำหนด properties ต่าง ๆ
myTextArea.move(20,10);
myTextArea.setSize(200,30);
myTextArea.text = "Select choice";
//loop สร้าง instance ของ RadioButton
for(var i:Number=1;i<4;i++){
this.createClassObject(mx.controls.RadioButton,"myRadioButton"+i,i);
_root["myRadioButton"+i].move(20,(20*i)+40);
_root["myRadioButton"+i].label = "RadioButton"+i;
//set groupName ของ RadioButton พวกนี้
_root["myRadioButton"+i].groupName = "choiceGroup";
}
//สร้าง instance ของ button
this.createClassObject(mx.controls.Button,"myButton",10);
myButton.move(20,150);
myButton.label = "ok";
//สร้าง listener เพื่อมารับ event ของ button
var myListener:Object = new Object();
myListener.click = function(){
var selectedRadioButton = choiceGroup.selection;
myTextArea.text = "you select "+selectedRadioButton.label;
}
myButton.addEventListener("click",myListener);
posted by flashas, at 2006-May-29 01:21:15
CheckBox Component [2]
จาก Entry ที่แล้ว เราได้สร้าง checkBox component จากการวนลูป ซึ่งชื่อของ instance มันก็จะอยู่ในรูปแบบ "xxxN"
Q : แล้วทีนี้ ถ้าจะใช้ checkBox เยอะ แต่ให้มันมีชื่อในแบบฉบับของตัวเองแล้วเวลา check จะ check ยังไง ?
A : ทำเป็น group แล้วใช้ condition check group ดังนี้
1.ลาก component Label,CheckBox,Button ไปไว้ใน Library
2.ใส่ code ดังนี้ที่ Frame แรก
//create label and set its properties.
this.createClassObject(mx.controls.Label,"myLabel",10);
myLabel.move(20,0);
myLabel.autoSize = "left";
myLabel.text = "nothing is checked";
//create checkboxs and set thier properties.
this.createClassObject(mx.controls.CheckBox,"firstBox",1);
firstBox.move(20,20);
firstBox.label = "1 st checkbox";
this.createClassObject(mx.controls.CheckBox,"secondBox",2);
secondBox.move(20,40);
secondBox.label = "2 nd checkbox";
this.createClassObject(mx.controls.CheckBox,"thirdBox",3);
thirdBox.move(20,60);
thirdBox.label = "3 rd checkbox";
//create button and set its properties
this.createClassObject(mx.controls.Button,"myButton",4);
myButton.move(20,80);
myButton.label = "OK";
//set group name ของแต่ละ checkbox
var GROUP_NAME = "groupname";
firstBox.group = GROUP_NAME;
secondBox.group = GROUP_NAME;
thirdBox.group = GROUP_NAME;
var myForm = this;
//create Listener for listen events
var myListener:Object = new Object();
//listener listen event "click"
myListener.click = function(){
var showText:String = "";
var hasCheck:Boolean = false;
//loop throught in myForm
for(var i in myForm){
var current_Item = myForm[i];
//check group ของ item ปัจจุบัน ที่ถูกเลือก ว่า มี grop เป็น "groupname" หรือไม่
if(current_Item.group == GROUP_NAME && current_Item.selected){
showText += current_Item.label+" ";
hasCheck = true;
}
}
if(hasCheck){
myLabel.text = showText+" is checked";
}else{
myLabel.text = "nothing is checked";
}
}
//addEventListener to myButton
myButton.addEventListener("click",myListener);