7 Replies Latest reply on Nov 11, 2010 11:42 AM by Peter deHaan

    how, a parameter of a function, give a "setFocus ()"?

    giba_f

      in this example:

       

      private function Function(_DatGri:DataGrid) {
          _DatGri.setFocus();
      }

       

       

      Thanks !!!

       

      Gilberto

        • 1. Re: how, a parameter of a function, give a "setFocus ()"?
          GordonSmith Level 4

          I don't understand your question. This function expects a DataGrid to be passed to it; it calls setFocus() on whatever DataGrid is passed in, causing this DataGrid to get keyboard events. Does that help?

           

          Gordon Smith

          Adobe Flex SDK Team

          • 2. Re: how, a parameter of a function, give a "setFocus ()"?
            giba_f Level 1

            I will illustrate this best:

             

            I have several dataGrid; and each with a search option, which calls the function Find (), and that when found the item, I would focus on dadagrid, on the item found.

             

            below, the source code to illustrate:

             

             

            <mx:HBox width="100%" height="30" horizontalAlign="center" verticalAlign="middle" >
                <mx:Label text="Search:"/>
                <mx:ComboBox  width="165"     id="OpcCity" dataProvider="{aSearch}"  />
                <mx:TextInput width="185"     id="ChaCity" />
                <mx:Button width="100" label="Search" click="Find(OpcCity.selectedIndex,ChaCity.text,aCity,BrwCity)" />
                <mx:DataGrid id="BrwCity" dataProvider="{aCity}" > 
                    <mx:columns>
                        <mx:DataGridColumn  width="80"  headerText="Name"       dataField="name"    />
                        <mx:DataGridColumn  width="80"  headerText="Country"    dataField="country" />
                        <mx:DataGridColumn  width="70"  headerText="Street"     dataField="street"  />
                    </mx:columns>
                </mx:DataGrid>
            </mx:HBox>

             

             

            <mx:HBox width="100%" height="30" horizontalAlign="center" verticalAlign="middle" >
                <mx:Label text="Search:"/>
                <mx:ComboBox  width="165"     id="OpcName" dataProvider="{aSearch}"  />
                <mx:TextInput width="185"     id="ChaName" />
                <mx:Button width="100" label="Search" click="Find(OpcName.selectedIndex,ChaName.text,aName,BrwName)" />
               
                <mx:DataGrid id="BrwName" dataProvider="{aName}" > 
                    <mx:columns>
                        <mx:DataGridColumn  width="80"  headerText="Cod"        dataField="cod"     />
                        <mx:DataGridColumn  width="80"  headerText="Name"       dataField="name"    />
                        <mx:DataGridColumn  width="70"  headerText="Old"        dataField="old"     />
                    </mx:columns>
                </mx:DataGrid>
            </mx:HBox>

             

             

            private function Find(_Item:int,_Chave:String,_Array:ArrayCollection,_DatGri:DataGrid) {
                _Array.sort = new Sort();
                _Array.sort.fields = [ new SortField( aSearch.getItemAt( _Item ).field , false, false ) ];
                _Array.refresh();
                for ( var i:int=0 ; i < _Array.length ; i++) {
                    if ( _Array.getItemAt(i)[ aSearch.getItemAt( _Item ).field ] == _Chave ) {
                        nRet = i;
                        _DatGri.selectedItem = i;  // this not working
                        _DatGri.setFocus();           // this also not working
                        break;
                    }
                }
            }

             

            Thank You Very Much !!!

             

            Gilberto

             

            • 3. Re: how, a parameter of a function, give a "setFocus ()"?
              Peter deHaan Level 4

              Shouldn't the "selectedItem" be "selectedIndex" since "i" is an int?

               

                          _DatGri.selectedItem = i;  // this not working
                          _DatGri.setFocus();           // this also not working

              • 4. Re: how, a parameter of a function, give a "setFocus ()"?
                giba_f Level 1

                not the error, nither with the "selectedItem", nither with the "selectedIndex".

                 

                But the question that is not the focus of the item or position in the datagrid, even taking the "i" right.

                 

                thanks.

                 

                Gilberto

                • 5. Re: how, a parameter of a function, give a "setFocus ()"?
                  Peter deHaan Level 4

                  This works for me in Flex 3.5:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                  
                      <mx:Script>
                          <![CDATA[
                              protected function button1_clickHandler(evt:MouseEvent):void {
                                  dg.selectedIndex = 2;
                                  dg.setFocus();
                              }
                          ]]>
                      </mx:Script>
                  
                      <mx:Button click="button1_clickHandler(event);" />
                      <mx:DataGrid id="dg"
                          initialize="dg.dataProvider = Font.enumerateFonts(true).sortOn('fontName');" />
                  
                  </mx:Application>
                  
                  
                  
                  Clicking the Button control sets the selected index, and then pressing the up/down arrows changes the selected item in the grid (proving that it currently has focus).
                  Peter

                  • 6. Re: how, a parameter of a function, give a "setFocus ()"?
                    giba_f Level 1

                    Peter ??

                     

                     

                    that is what I need, with only difference of not being able to use the id "dg" from his example. because the function is called by several buttons, each in a different datagrid. why we need to pass the id of the datagrid by using the parameter and function setFocus () in this parameter received ... see my source demo up there ..

                    Thank you.

                     

                    Gilberto

                    • 7. Re: how, a parameter of a function, give a "setFocus ()"?
                      Peter deHaan Level 4

                      Sure, this still works for me in Flex 3.5:

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
                      
                          <mx:Script>
                              <![CDATA[
                                  protected function selectAndFocus(target:DataGrid, index:int):void {
                                      target.selectedIndex = index;
                                      target.setFocus();
                                  }
                              ]]>
                          </mx:Script>
                      
                          <mx:Button click="selectAndFocus(dg, 2);" />
                          <mx:DataGrid id="dg"
                              initialize="dg.dataProvider = Font.enumerateFonts(true).sortOn('fontName');" />
                      
                      </mx:Application>
                      
                      
                      
                      Peter