示例#1
0
        /// <summary>
        /// Change the focus of the grid.
        /// The calls order is:
        ///
        /// (the user select CellX)
        /// CellX.FocusEntering
        /// Grid.CellGotFocus(CellX),
        /// CellX.FocusEntered,
        /// [OnFocusRowEntered],
        /// [OnFocusColumnEntered]
        ///
        /// (the user select CellY),
        /// CellY.FocusEntering
        /// CellX.FocusLeaving
        /// Grid.CellLostFocus(CellX),
        /// [OnFocusRowLeaving],
        /// [OnFocusColumnLeaving],
        /// CellX.FocusLeft,
        /// Grid.CellGotFocus(CellY),
        /// CellY.FocusEntered,
        /// [OnFocusRowEntered],
        /// [OnFocusColumnEntered]
        ///
        /// Use Position.Empty to remove the focus cell.
        /// </summary>
        /// <param name="pCellToActivate"></param>
        /// <param name="pResetSelection">Reset the other selected cells.</param>
        /// <returns></returns>
        public virtual bool Focus(Position pCellToActivate, bool pResetSelection)
        {
            //If control key is pressed, enableMultiSelection is true and the cell that will receive the focus is not empty leave the cell selected otherwise deselect other cells
            bool deselectOtherCells = false;

            if (pCellToActivate.IsEmpty() == false && pResetSelection)
            {
                deselectOtherCells = true;
            }

            pCellToActivate = Grid.PositionToStartPosition(pCellToActivate);

            //Questo controllo è necessario altrimenti l'evento verrebbe scatenato due volte per la stessa cella
            if (pCellToActivate != ActivePosition)
            {
                //GotFocus Event Arguments
                Cells.ICellVirtual            newCellToFocus    = Grid.GetCell(pCellToActivate);
                CellContext                   newCellContext    = new CellContext(Grid, pCellToActivate, newCellToFocus);
                ChangeActivePositionEventArgs gotFocusEventArgs = new ChangeActivePositionEventArgs(ActivePosition, pCellToActivate);

                //LostFocus Event Arguments
                Cells.ICellVirtual            oldCellFocus       = Grid.GetCell(ActivePosition);
                CellContext                   oldCellContext     = new CellContext(Grid, ActivePosition, oldCellFocus);
                ChangeActivePositionEventArgs lostFocusEventArgs = new ChangeActivePositionEventArgs(ActivePosition, pCellToActivate);

                if (newCellToFocus != null)
                {
                    //Cell Focus Entering
                    Grid.Controller.OnFocusEntering(newCellContext, gotFocusEventArgs);
                    if (gotFocusEventArgs.Cancel)
                    {
                        return(false);
                    }

                    //If the cell can't receive the focus stop the focus operation
                    if (Grid.Controller.CanReceiveFocus(newCellContext, gotFocusEventArgs) == false)
                    {
                        return(false);
                    }
                }

                //If the focus is already insede the grid (bug maybe in another control) or the new cell is valid
                if (Grid.ContainsFocus || newCellToFocus != null)
                {
                    //This method cause any cell editor to leave the focus if the validation is ok, otherwise returns false. This is useful for 2 reason:
                    //	-To validate the editor
                    //	-To check if I can move the focus on another cell
                    bool canFocus = Grid.SetFocusOnCells(true);
                    if (canFocus == false)
                    {
                        return(false);
                    }
                }


                if (oldCellFocus != null)
                {
                    //Cell Focus Leaving
                    Grid.Controller.OnFocusLeaving(oldCellContext, lostFocusEventArgs);
                    if (lostFocusEventArgs.Cancel)
                    {
                        return(false);
                    }

                    //Cell Lost Focus
                    OnCellLostFocus(lostFocusEventArgs);
                    if (lostFocusEventArgs.Cancel)
                    {
                        return(false);
                    }
                }

                //Deselect previous selected cells
                if (deselectOtherCells)
                {
                    Clear();
                }

                bool success;
                if (newCellToFocus != null)
                {
                    //Cell Got Focus
                    OnCellGotFocus(gotFocusEventArgs);

                    success = (!gotFocusEventArgs.Cancel);
                }
                else
                {
                    success = true;
                }

                return(success);
            }
            else
            {
                return(true);
            }
        }