try the following code:-

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute"
creationComplete="selectindex()">

    <mx:ComboBox id="myCombo" dataProvider="{items}" width="121">
    </mx:ComboBox>

    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            [Bindable]
                   public var items:ArrayCollection = new ArrayCollection(
               [ {label:"Item 1", data:"item1"},
                 {label:"Item2", data:"item2"},
                 {label:"Item3", data:"item3"},
                 {label:"Item4", data:"item4"},
                 {label:"Item5", data:"item5"} ]);

                 private function selectindex():void
                 {
                     var selected: String = 'item3';
                     var i:int = 0;
                     var found:Boolean = false;
                     for (i=0;i<items.length;i++)
                     {
                         var obj:Object = items.getItemAt(i) as Object;
                         if (obj.data == selected)
                         {
                             found = true;
                             break;
                         }
                     }
                     if (found)
                     {
                         myCombo.selectedIndex = i;
                     }
                     else
                     {
                         myCombo.prompt = "cannot find the data";
                         myCombo.selectedIndex = -1;
                     }
                    //myCombo.selectedItem = obj;
                 }
        ]]>
    </mx:Script>
</mx:Application>



Akshar Kaul


On Fri, Jul 30, 2010 at 13:05, JT <[email protected]> wrote:

> @Khalid I'll try what you said.
>
> @Rakesh, buddy I know that using selectedIndex we can select an item
> but I dont have index, I have the data of arraycollection i.e. in my
> variable I have item3 not 2.
>
> var selected: String = ''item3; (this is what I have)
> var selected: int= 2; (I dont have this)
>
> So using item3 which is the data field of arraycollection, how can I
> select Item3 in combobox.
>
> I hope u got what I want to say.
>
> Thanks,
> JT
>
>
> On Jul 30, 7:06 am, rakesh amety <[email protected]> wrote:
> > In combo box creation complete add this code myCombo.selectedIndex = 2
> >
> > On Fri, Jul 30, 2010 at 1:24 AM, JT <[email protected]> wrote:
> > > It friends,
> >
> > > How can I load a drop down box with pre-selected item?
> >
> > > Here is my dropdown box
> >
> > > [Bindable]
> > >            public var items:ArrayCollection = new ArrayCollection(
> > >                [ {label:"Item 1", data:"item1"},
> > >                  {label:"Item2", data:"item2"},
> > >                  {label:"Item3", data:"item3"},
> > >                  {label:"Item4", data:"item4"},
> > >                  {label:"Item5", data:"item5"} ]);
> >
> > > <mx:ComboBox id="myCombo" dataProvider="{items}" width="121"></
> > > mx:ComboBox>
> >
> > > Now lets say I have a variable which have a value item3, than I want
> > > that when the mxml loads the Item 3 should be pre selected, I am doing
> > > something like this
> >
> > > var selected: String = ''item3;
> > > myCombo.selectedItem.data = selected;
> >
> > > Could you please suggest what I am doing wrong, or what should I do ?
> >
> > > Thanks,
> > > Jaswant
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Flex India Community" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<flex_india%[email protected]>
> <flex_india%[email protected]<flex_india%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/flex_india?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Flex India Community" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<flex_india%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/flex_india?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en.

Reply via email to