public 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);
//Check to see if the value is changed (note that I use the internal variable to see the actual stored value)
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);
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 new cell is valid I check if I can move the focus inside the grid
if (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.Focus();
if (canFocus == false)
return false;
}
//If there is a cell with the focus fire the focus leave events
if (IsActivePositionValid())
{
//LostFocus Event Arguments
Cells.ICellVirtual oldCellFocus = Grid.GetCell(ActivePosition);
CellContext oldCellContext = new CellContext(Grid, ActivePosition, oldCellFocus);
ChangeActivePositionEventArgs lostFocusEventArgs = new ChangeActivePositionEventArgs(ActivePosition, pCellToActivate);
//Cell Focus Leaving
Grid.Controller.OnFocusLeaving(oldCellContext, lostFocusEventArgs);
if (lostFocusEventArgs.Cancel)
return false;
//Cell Lost Focus
OnCellLostFocus(lostFocusEventArgs);
if (lostFocusEventArgs.Cancel)
return false;
}
else
{
//Reset anyway the actual value. This can happen when there is an ActivePosition but it is not more valid (outside the valid range maybe when removing some cells)
// NOTE: in this case the focus event are not executed
m_ActivePosition = Position.Empty;
}
//Deselect previous selected cells
if (deselectOtherCells)
ResetSelection(false);
bool success;
if (newCellToFocus != null)
{
//Cell Got Focus
OnCellGotFocus(gotFocusEventArgs);
success = (!gotFocusEventArgs.Cancel);
}
else
{
success = true;
}
//Fire a change event
OnSelectionChanged(EventArgs.Empty);
return success;
}
else
{
if (pCellToActivate.IsEmpty() == false)
{
//I check if the grid still has the focus, otherwise I force it
if (Grid.ContainsFocus)
return true;
else
return Grid.Focus();
}
else
return true;
}
}