dcsimg
A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: ComboBox set selectedItem

  1. #1
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84

    ComboBox set selectedItem

    How do I set this?

    Flash documentation says this is possible, but cb.selectedItem={label:'labeltext',data:'data'}; is a no go for me. Maybe I'm missing something obvious?

    Setting selectedIndex works great, but this should work too.

    Can anyone get this to work?

    Thanks.

    sphoenixee

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    ComboBoxes I've come to find are very very quirky, There are ways around them though. If you just want the displayed option to change, you can change the label text by resetting the prompt, or, like you found, by using selectedIndex.

    Best way for me to think of a solution is for you to explain a bit why you want to do this and in what instance you want to do it.

  3. #3
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    Surely there is a way to change it using selectedItem? Or is this a bug within Flash?

    For me, I am loading data from XML and changing the selectedItem to match the data contained within that XML.

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    There is a method called itemToLabel that you can explore if you'd like. Now that will work when you click on an item. There may also be a way to set it to go to a certain index or label within the dataprovider of the combobox, if you'd like to explore that avenue or approach. If you want to attach an example, I can try to make you an example of what your trying to do.

  5. #5
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    I now realize I was being unclear in my prior requests.

    Here is what I want to do:
    Say I have 1000 XML nodes called 'node' with two properties, 'name' which is free input, and 'type' which is limited to four types.
    I have a combobox with four items, which correspond to the four types of the XML nodes.
    I am making a viewer to view 'node's of the XML. The user can browse the 'node's by clicking on back/forward buttons, and a textfield on the screen will display 'name' and a combobox display the 'type.'
    The user can also edit the 'name' and 'type.' The reason the combobox (versus textfield) is used for 'type' is to make sure user input is correct.
    I do not want to change the items in the combobox.

    Sorry about the unclear explanation earlier. Hopefully this makes it more clear.

    Thank you for all your help.

    If you still want an example, I'll attach one.

    Should I assume that set selectdedItem does not work? Has anyone gotten this to work?

  6. #6
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    The selectedItem property is set when the combobox list value is changed. You cannot use that to change the label and data in the combobox. Use
    replaceItemAt
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  7. #7
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    Again, I do not want to change the data or labels that the comboBox contains. I felt this was made clear in post 5. I want to change what is selected. In other words, it's like how you can write selectedIndex=5;, and the selected item will become the 5th item; but instead, I want to do this using selectedItem.

    At this point, it's more of a theoretical question for me, but it would be nice to know if it can be done or if Flash documentation is wrong.

    Thanks.

  8. #8
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    You've got to use selectedIndex, unless you want to provide a new dataprovider with new array everytime you want it to change.

  9. #9
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    I see. So, I take Flash documentation is wrong when it says:
    Implementation
    public function get selectedItem():Object
    public function set selectedItem(value:Object):void

    If anyone gets the setter to work, let me know, otherwise, I consider this a dead end and/or resolved.

  10. #10
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Well, you can do it like this here:

    Code:
    import fl.controls.*;
    import fl.data.*;
    import fl.controls.listClasses.*;
    var c1:ComboBox = new ComboBox();
    var dp:Array = new Array();
    for (var i:Number = 0; i<21; i++) {
    	dp.push("Item "+i);
    }
    c1.dataProvider = new DataProvider(dp);
    c1.selectedItem = c1.getItemAt(4);
    addChild(c1);
    With this example that would make your box start out with "Item 4" in the label field.
    Last edited by jweeks123; 07-18-2008 at 01:17 PM.

  11. #11
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    That works quite nicely

    Thank you.

    But if I write c1.selectedItem={label:'Item 4',data:'Item 4'}; it does not work. Why is this? Is that not the data contained at c1.getItemAt(4)?

  12. #12
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    This will also work:

    Granted they run along the same concept and that should work, but mistake on adobes part I'd be willing to wager that doesn't allow that. Anyway, try these and let me know what you think. Personally I'd store all the items in an array as shown below, that way you can reference them at any time.

    Code:
    import fl.controls.*;
    import fl.data.*;
    import fl.controls.listClasses.*;
    var c1:ComboBox = new ComboBox();
    var dp:Array = new Array();
    for (var i:Number = 0; i<21; i++) {
    	dp.push("Item "+i);
    }
    c1.dataProvider = new DataProvider(dp);
    //new code here
    //****************************************************
    var itemArr:Array = new Array();
    for(var a:Number = 0; a<c1.length; a++) {
    	itemArr.push(c1.getItemAt(a));
    }
    c1.selectedItem = itemArr[3];
    //****************************************************
    addChild(c1);
    as well as this:

    Code:
    import fl.controls.*;
    import fl.data.*;
    import fl.controls.listClasses.*;
    var c1:ComboBox = new ComboBox();
    var dp:Array = new Array();
    for (var i:Number = 0; i<21; i++) {
    	dp.push("Item "+i);
    }
    c1.dataProvider = new DataProvider(dp);
    //Start new code here
    //*******************************************************
    var obj1:Object = c1.getItemAt(4);
    c1.selectedItem = obj1;
    //*******************************************************
    addChild(c1);

  13. #13
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    Thanks jweeks, those were really helpful.

    So selectedItem setter works fine.

    I tried the code you wrote and some of my own. To rehash, what I want to do is to change selectedItem, not using the index, but using the label and the data.
    As you wrote, we can do this by putting the information in an array, and then getting indices out of the array, I presumed by using using things like array[index].property=='selected property name'; return index. Note also though, that one does not need the array to do this; we might as well run a for (var index=0;index<dp.length;index++){if (dp.getItemAt(index)=='selected property name'){return index}}

    Also, I have not been able to get array.indexOf({label:'my label',data:'my data'}) kind of code to work.

    Back to my original question, I am still wondering why cb.selectedItem={label:'label x',data:'some data'} does not work.
    Now that we know selectedItem works, and that cb.selectedItem works, the only possible problem is that {label:'label x',data:'some data'} is faulty. Indeed this is the problem. Examine the following.

    import fl.controls.*;
    import fl.data.*;
    import fl.controls.listClasses.*;
    var arr:Array=new Array();
    var dpataProvider=new DataProvider();

    var obj:Object={label:'My label',data:'My data'};
    var obj2:Object={label:'My label',data:'My data'};
    arr.push(obj);
    dp.addItem(obj);

    trace(arr[arr.length-1]==dp.getItemAt(dp.length-1)); //TRUE
    trace(arr[arr.length-1]==obj); //TRUE
    trace(dp.getItemAt(dp.length-1)==obj); //TRUE
    trace(obj=={label:'My label',data:'My data'});

    //The surprise (for me) begins here.
    trace(arr[arr.length-1]=={label:'My label',data:'My data'}); //FALSE
    trace(dp.getItemAt(dp.length-1)=={label:'My label',data:'My data'}); //FALSE
    trace(obj==obj2); //FALSE
    trace(dp.getItemAt(dp.length-1)==obj2); //FALSE
    trace(arr[arr.length-1]==obj2); //FALSE
    trace(obj2=={label:'My label',data:'My data'}); //FALSE
    arr.push(obj2);
    dp.addItem(obj2);
    trace(dp.getItemAt(dp.length-1)==obj2); //TRUE
    trace(arr[arr.length-1]==obj2); //TRUE
    trace(obj2=={label:'My label',data:'My data'}); //FALSE
    trace(arr[0]==arr[1]); //FALSE
    trace(obj2==obj); //FALSE
    trace(dp.getItemAt(0)==dp.getItemAt(1)); //FALSE

    So essentially, we assign a value to a variable, but that variable is not equivalent to the value....how does this work?

    I truly think I am misunderstanding some fundamental part of AS3 here. Help!
    Last edited by sphoenixee; 07-21-2008 at 10:38 PM.

  14. #14
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    I haven't tried this yet, but I've got a thought. That thought being, is the object your trying to set as the selectedItem in the List of the comboBox? Perhaps that could be the issue at hand? And yes I do agree that flash is having an issue with this. I will continue to study more on the issue though.

  15. #15
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Running along with that thought I analyzed your code and tested some of my own to get some conclusions:

    1. The selected Item must be part of the list in which your calling.
    2. In the selectedItem call you must call the name of the object within the combobox, so if you define all your labels to be display as objects, then add all the objects as items to a dataprovider and then set the dataprovider as the comboboxes dataprovider, you can call the name of the object you originally defined.
    3. Adobe way over complicated this, or way under documented it, LOL.

    Anyway, here's what I tried:

    Code:
    import fl.controls.*;
    import fl.data.*;
    import fl.controls.listClasses.*;
    var arr:Array=new Array();
    
    var dp:DataProvider=new DataProvider();
    
    var obj1:Object={label:'My label 1',data:'My data 1'};
    var obj2:Object={label:'My label 2',data:'My data 2'};
    var obj3:Object={label:'My label 3',data:'My data 3'};
    var obj4:Object={label:'My label 4',data:'My data 4'};
    dp.addItem(obj1);
    dp.addItem(obj2);
    dp.addItem(obj3);
    dp.addItem(obj4);
    var cb:ComboBox = new ComboBox();
    addChild(cb);
    //works
    cb.dataProvider = new DataProvider(dp);
    cb.selectedItem = obj3;
    //doesn't work
    //var selObj:Object = {label:'My label 3',data:'My data 3'};
    //cb.selectedItem = selObj;
    //
    //cb.selectedItem = {label:'My label 3',data:'My data 3'};
    //
    //cb.selectedItem = Object({label:'My label 3',data:'My data 3'});

  16. #16
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    I agree with your conclusions.

    I think the fact that you have to use the original object is not a good idea though. In fact, I can hardly see any advantage to using it, but there is the clear disadvantage of not being able to use selectedItem with the label and data.

    Ah well. There's other ways to manipulate the selected item so at least we're not at a dead end.

    Thanks so much for all your help

  17. #17
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    No Problem, only sorry we couldn't get to a better solution.

  18. #18
    Junior Member
    Join Date
    Dec 2010
    Posts
    1

    this code worked!

    Well, I did a similar sintaxe and it works. I don´t know it was because the key or the item type, but Its works!

    //doesn't work
    //var selObj:Object = {label:'My label 3',data:'My data 3'};
    //cb.selectedItem = selObj;
    //
    //cb.selectedItem = {label:'My label 3',data:'My data 3'};
    //
    //cb.selectedItem = Object({label:'My label 3',data:'My data 3'});


    //Work
    var selObj = {label:"My label", index:"key01"};
    //add this obj
    cb.addItem(selObj);
    //select this object
    cb.selectedItem = selObj;

  19. #19
    Junior Member
    Join Date
    Jan 2006
    Posts
    2

    i dont know what the big deal is.

    I changed my selected item with

    combo.selectedIndex = 5;

  20. #20
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    You can do

    comboboxName.selectedItem.data = data
    comboboxName.selectedItem.label = label


    and for an argument

    if (comboboxName.selectedItem.data = data){
    whatever you want
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center