Ejemplo n.º 1
0
        /// <summary>
        /// Move the active cell (focus), moving the row and column as specified. Returns true if the focus can be moved.
        /// Returns false if there aren't any cell to move.
        /// </summary>
        /// <param name="start"></param>
        /// <param name="rowShift"></param>
        /// <param name="colShift"></param>
        /// <returns></returns>
        public bool MoveActiveCell(Position start, int rowShift, int colShift)
        {
            Position newPosition = Position.Empty;

            //If there isn't a current active cell I try to put the focus on the 0, 0 cell.
            if (start.IsEmpty())
            {
                newPosition = new Position(0, 0);
                if (CanReceiveFocus(newPosition))
                {
                    return(Focus(newPosition));
                }
                else
                {
                    start       = newPosition;
                    newPosition = Position.Empty;
                }
            }

            int currentRow = start.Row;
            int currentCol = start.Column;

            currentRow += rowShift;
            currentCol += colShift;

            while (newPosition.IsEmpty() && currentRow < Grid.Rows.Count && currentCol < Grid.Columns.Count &&
                   currentRow >= 0 && currentCol >= 0)
            {
                newPosition = new Position(currentRow, currentCol);

                //verifico che la posizione di partenza non coincida con quella di focus, altrimenti significa che ci stiamo spostando sulla stessa cella perchè usa un RowSpan/ColSpan
                if (Grid.PositionToStartPosition(newPosition) == start)
                {
                    newPosition = Position.Empty;
                }
                else
                {
                    if (CanReceiveFocus(newPosition) == false)
                    {
                        newPosition = Position.Empty;
                    }
                }

                currentRow += rowShift;
                currentCol += colShift;
            }

            if (newPosition.IsEmpty() == false)
            {
                return(Focus(newPosition));
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 2
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);
            }
        }