/// <summary> /// Process Delete, Ctrl+C, Ctrl+V, Up, Down, Left, Right, Tab keys /// </summary> /// <param name="e"></param> public virtual void ProcessSpecialGridKey(KeyEventArgs e) { if (e.Handled) return; bool enableArrows,enableTab,enablePageDownUp; enableArrows = enableTab = enablePageDownUp = false; if ( (SpecialKeys & GridSpecialKeys.Arrows) == GridSpecialKeys.Arrows) enableArrows = true; if ( (SpecialKeys & GridSpecialKeys.PageDownUp) == GridSpecialKeys.PageDownUp) enablePageDownUp = true; if ( (SpecialKeys & GridSpecialKeys.Tab) == GridSpecialKeys.Tab) enableTab = true; bool enableEscape = false; if ( (SpecialKeys & GridSpecialKeys.Escape) == GridSpecialKeys.Escape) enableEscape = true; bool enableEnter = false; if ( (SpecialKeys & GridSpecialKeys.Enter) == GridSpecialKeys.Enter) enableEnter = true; // AlanP: March 2014 - Work out which is the first column that is displayed after any fixed columns // We use firstActiveColumn in multiple places below int firstActiveColumn = this.FixedColumns; while ((firstActiveColumn < this.Columns.Count - 1) && !((ColumnInfoCollection)this.Columns)[firstActiveColumn].Visible) { firstActiveColumn++; } #region Processing keys // AlanP: Nov 2013. Made some changes to the behaviours of ENTER and Esc so we move to the next editable cell // Also there was a bug in the code that checked if a cell IsEditing // AlanP: Aug 2014. We have 2 styles of grid. The main one for displaying data tables and a spacialised one for Local Partner Data // The specialised one uses Controllers, where the standard one uses optional Editors CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); ICellVirtual contextCell = focusCellContext.Cell; //if (focusCellContext.Cell != null && focusCellContext.IsEditing()) // AlanP: Nov 2013. Replcaed the line above with this because focusCellContext.IsEditing() returns false for a reason I don't understand. bool isEditorEditing = (contextCell != null && contextCell.Editor != null && contextCell.Editor.IsEditing); bool useSimplifiedTabEnter = ((SpecialKeys & GridSpecialKeys.SimplifiedTabEnter) == GridSpecialKeys.SimplifiedTabEnter); //Escape if (e.KeyCode == Keys.Escape && enableEscape) { if (isEditorEditing) { if (focusCellContext.EndEdit(true)) { // Move to next editable cell if there is one bool bFound = false; do { Selection.MoveActiveCell(0, 1, 0, int.MinValue); ICellVirtual nextContextCell = (new CellContext(this, Selection.ActivePosition)).Cell; bFound = (nextContextCell != null && nextContextCell.Editor != null && nextContextCell.Editor.EditableMode != EditableMode.None); } while (Selection.ActivePosition.Column != firstActiveColumn && !bFound); e.Handled = true; } } } //Enter if (e.KeyCode == Keys.Enter && enableEnter) { // This is the main special key for dealing with edit-in-place in OpenPetra // Pressing ENTER always completes an outstanding edit if (isEditorEditing) { focusCellContext.EndEdit(false); } if (useSimplifiedTabEnter) { return; } bool isLastColumn = (Selection.ActivePosition.Column == this.Columns.Count - 1); bool isFirstColumn = (Selection.ActivePosition.Column == firstActiveColumn); if (isFirstColumn) { // If we are on the first column and columns exist to the right then ENTER moves us to the next editable cell // or back to column 0 if there are no more editable cells if (!isLastColumn) { bool bFound = false; do { Selection.MoveActiveCell(0, 1, 0, int.MinValue); ICellVirtual nextContextCell = (new CellContext(this, Selection.ActivePosition)).Cell; bFound = (nextContextCell != null && nextContextCell.Editor != null && nextContextCell.Editor.EditableMode != EditableMode.None); } while (Selection.ActivePosition.Column != firstActiveColumn && !bFound); e.Handled = bFound; } else { e.Handled = false; } } else { // If we are NOT on the first column we go somewhere else depending on SHIFT or CTRL if (e.Modifiers == Keys.Shift) { // Go down 1 row in the current column and then ultimately back to column 0 on the last row this.Selection.MoveActiveCell(1, 0, 0, int.MinValue); } else if (e.Modifiers == Keys.Control) { // Go down 1 row in column 0 this.Selection.MoveActiveCell(1, -this.Selection.ActivePosition.Column, 0, int.MinValue); } else { // Neither SHIFT nor CTRL so go across to the right in the current row to the next editable control // or go back to column 0 in the current row bool bFound = false; do { Selection.MoveActiveCell(0, 1, 0, int.MinValue); ICellVirtual nextContextCell = (new CellContext(this, Selection.ActivePosition)).Cell; bFound = (nextContextCell != null && nextContextCell.Editor != null && nextContextCell.Editor.EditableMode != EditableMode.None); } while (Selection.ActivePosition.Column != firstActiveColumn && !bFound); } e.Handled = true; } return; } //Tab if (e.KeyCode == Keys.Tab && enableTab) { // All we need to do is close down any edit // Pressing TAB always completes an outstanding edit if (isEditorEditing) { focusCellContext.EndEdit(false); } if (useSimplifiedTabEnter) { // Carry on below... } else { // There is no controller on the cell (we have a controller on the special grid on LocalData screen) // That screen behaves differently. // For all normal grids be sure to go back to column 0 in the current row if (Selection.ActivePosition.Column != firstActiveColumn) { this.Selection.Focus(new Position(Selection.ActivePosition.Row, firstActiveColumn), true); } return; } } #endregion #region Navigate keys: arrows, tab and PgDown/Up var shiftPressed = e.Modifiers == Keys.Shift; var resetSelection = shiftPressed == false; // AlanP: Sep 2013. We need this because the first time we press SHIFT we may be on any row in the grid // This ensures that, if m_firstCellShiftSelected has never been set, it gets the current position before any move if (shiftPressed && (m_firstCellShiftSelected.Row < this.FixedRows)) { m_firstCellShiftSelected = Selection.ActivePosition; } if (e.KeyCode == Keys.Down && enableArrows) { Selection.MoveActiveCell(1, 0, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Up && enableArrows) { Selection.MoveActiveCell(-1, 0, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Right && enableArrows && SelectionMode == GridSelectionMode.Column) { // AlanP: Jan 2014 support left and right when in column selection mode only // (actually our column mode screens cannot take advantage of this feature because the ActivePosition has intentionally been set to -1,-1) Selection.MoveActiveCell(0, 1, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Left && enableArrows && SelectionMode == GridSelectionMode.Column) { Selection.MoveActiveCell(0, -1, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Tab && enableTab) { //If the tab failed I automatically select the next control in the form (SelectNextControl) if (e.Modifiers == Keys.Shift) // backward { if (Selection.MoveActiveCell(0, -1, -1, int.MaxValue) == false) FindForm().SelectNextControl(this, false, true, true, true); e.Handled = true; } else //forward { if (Selection.MoveActiveCell(0, 1, 1, int.MinValue) == false) FindForm().SelectNextControl(this, true, true, true, true); e.Handled = true; } } else if ( (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown) && enablePageDownUp) { //MICK(18): I commented out this part - focusing is done within my CustomScrollPageDown() and CustomScrollPageUp() //Point focusPoint = PositionToRectangle(Selection.ActivePosition).Location; //focusPoint.Offset(1, 1); //in modo da entrare nella cella if (e.KeyCode == Keys.PageDown) CustomScrollPageDown(); else if (e.KeyCode == Keys.PageUp) CustomScrollPageUp(); //MICK(18): I commented out this part - focusing is done within my CustomScrollPageDown() and CustomScrollPageUp() //Position newPosition = PositionAtPoint(focusPoint); //if (Selection.CanReceiveFocus(newPosition)) // Selection.Focus(newPosition, true); e.Handled = true; } //ALAN - July 2013. I added '&& e.handled' to the if clause // This fixed several buggy behaviours // Pressing SHIFT on its own caused a cascade of Focus events and selection_changed events - the knock-on effects were amazing! // 1. Just pressing SHIFT would cause multiple highlighted rows to become just one highlighted row // 2. SHIFT+mouse click did not work. if (shiftPressed && e.Handled && !useSimplifiedTabEnter) { // AlanP: Sep 2013 Inhibit the change event on this one Selection.ResetSelection(true, true); // This fires the selection_changed event, then we are done Selection.SelectRange(new Range(m_firstCellShiftSelected, Selection.ActivePosition), true); } #endregion #region Clipboard RangeRegion selRegion = (ClipboardUseOnlyActivePosition ? new RangeRegion(Selection.ActivePosition) : Selection.GetSelectionRegion()); //Paste if (e.Control && e.KeyCode == Keys.V) { PerformPaste(selRegion); e.Handled = true; } //Copy else if (e.Control && e.KeyCode == Keys.C) { PerformCopy(selRegion); e.Handled = true; } //Cut else if (e.Control && e.KeyCode == Keys.X) { PerformCut(selRegion); e.Handled = true; } //Delete else if (e.KeyCode == Keys.Delete) { PerformDelete(selRegion); e.Handled = true; } #endregion }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); //verifico che l'eventuale edit sia terminato altrimenti esco if (Selection.ActivePosition.IsEmpty() == false) { CellContext focusCell = new CellContext(this, Selection.ActivePosition); if (focusCell.Cell != null && focusCell.IsEditing()) { if (focusCell.EndEdit(false) == false) return; } } //scateno eventi di MouseDown Position position = PositionAtPoint(new Point(e.X, e.Y)); if (position.IsEmpty() == false) { Cells.ICellVirtual cellMouseDown = GetCell(position); if (cellMouseDown != null) { // AlanP: Nov 2013. Added this if/else clause to set the position to the first column unless the click is on an editable cell if (cellMouseDown.Editor != null && cellMouseDown.Editor.EditableMode == EditableMode.Focus) { // The position is ok CellContext newFocusCell = new CellContext(this, position); newFocusCell.StartEdit(); } else if (position.Row >= this.FixedRows) { // AlanP: Jan 2014. Added this test that we are doing full row selection because on some grids we do column selection if (this.SelectionMode == GridSelectionMode.Row) { // always imagine that the click was on the first column // AlanP: March 2014 - the column is now the first VISIBLE column (in the normal sense, not visible in the display window) // AP screens may or may not display a checkbox column in column 0 int column = this.FixedColumns; while (column < this.Columns.Count - 1 && !((ColumnInfoCollection)this.Columns)[column].Visible) { column++; } position = new Position(position.Row, column); } } ChangeMouseDownCell(position, position); //Cell.OnMouseDown CellContext cellContext = new CellContext(this, position, cellMouseDown); Controller.OnMouseDown(cellContext, e); } } else ChangeMouseDownCell(Position.Empty, Position.Empty); }
/// <summary> /// Process Delete, Ctrl+C, Ctrl+V, Up, Down, Left, Right, Tab keys /// </summary> /// <param name="e"></param> public virtual void ProcessSpecialGridKey(KeyEventArgs e) { if (e.Handled) return; bool enableArrows,enableTab,enablePageDownUp; enableArrows = enableTab = enablePageDownUp = false; if ( (SpecialKeys & GridSpecialKeys.Arrows) == GridSpecialKeys.Arrows) enableArrows = true; if ( (SpecialKeys & GridSpecialKeys.PageDownUp) == GridSpecialKeys.PageDownUp) enablePageDownUp = true; if ( (SpecialKeys & GridSpecialKeys.Tab) == GridSpecialKeys.Tab) enableTab = true; bool enableEscape = false; if ( (SpecialKeys & GridSpecialKeys.Escape) == GridSpecialKeys.Escape) enableEscape = true; bool enableEnter = false; if ( (SpecialKeys & GridSpecialKeys.Enter) == GridSpecialKeys.Enter) enableEnter = true; #region Processing keys //Escape if (e.KeyCode == Keys.Escape && enableEscape) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { if (focusCellContext.EndEdit(true)) e.Handled = true; } } //Enter if (e.KeyCode == Keys.Enter && enableEnter) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { focusCellContext.EndEdit(false); e.Handled = true; } } //Tab if (e.KeyCode == Keys.Tab && enableTab) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { //se l'editing non riesce considero il tasto processato // altrimenti no, in questo modo il tab ha effetto anche per lo spostamento if (focusCellContext.EndEdit(false) == false) { e.Handled = true; return; } } } #endregion #region Navigate keys: arrows, tab and PgDown/Up if (e.KeyCode == Keys.Down && enableArrows) { Selection.MoveActiveCell(1, 0); e.Handled = true; } else if (e.KeyCode == Keys.Up && enableArrows) { Selection.MoveActiveCell(-1, 0); e.Handled = true; } else if (e.KeyCode == Keys.Right && enableArrows) { Selection.MoveActiveCell(0, 1); e.Handled = true; } else if (e.KeyCode == Keys.Left && enableArrows) { Selection.MoveActiveCell(0, -1); e.Handled = true; } else if (e.KeyCode == Keys.Tab && enableTab) { //If the tab failed I automatically select the next control in the form (SelectNextControl) if (e.Modifiers == Keys.Shift) // backward { if (Selection.MoveActiveCell(0, -1, -1, int.MaxValue) == false) FindForm().SelectNextControl(this, false, true, true, true); e.Handled = true; } else //forward { if (Selection.MoveActiveCell(0, 1, 1, int.MinValue) == false) FindForm().SelectNextControl(this, true, true, true, true); e.Handled = true; } } else if ( (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown) && enablePageDownUp) { Point focusPoint = PositionToRectangle(Selection.ActivePosition).Location; focusPoint.Offset(1, 1); //in modo da entrare nella cella if (e.KeyCode == Keys.PageDown) CustomScrollPageDown(); else if (e.KeyCode == Keys.PageUp) CustomScrollPageUp(); Position newPosition = PositionAtPoint(focusPoint); if (Selection.CanReceiveFocus(newPosition)) Selection.Focus(newPosition, true); e.Handled = true; } #endregion #region Clipboard bool pasteEnabled = (ClipboardMode & ClipboardMode.Paste) == ClipboardMode.Paste; bool copyEnabled = (ClipboardMode & ClipboardMode.Copy) == ClipboardMode.Copy; bool cutEnabled = (ClipboardMode & ClipboardMode.Cut) == ClipboardMode.Cut; bool deleteEnabled = (ClipboardMode & ClipboardMode.Delete) == ClipboardMode.Delete; RangeRegion selRegion = Selection.GetSelectionRegion(); //Paste if (e.Control && e.KeyCode == Keys.V && pasteEnabled && selRegion.IsEmpty() == false) { RangeData rngData = RangeData.ClipboardGetData(); if (rngData != null) { Range rng = selRegion.GetRanges()[0]; Range destinationRange = rngData.FindDestinationRange(this, rng.Start); rngData.WriteData(this, destinationRange); e.Handled = true; Selection.ResetSelection(true); Selection.SelectRange(destinationRange, true); } } //Copy else if (e.Control && e.KeyCode == Keys.C && copyEnabled && selRegion.IsEmpty() == false) { Range rng = selRegion.GetRanges()[0]; RangeData data = new RangeData(); data.LoadData(this, rng, rng.Start, CutMode.None); RangeData.ClipboardSetData(data); e.Handled = true; } //Cut else if (e.Control && e.KeyCode == Keys.X && cutEnabled && selRegion.IsEmpty() == false) { Range rng = selRegion.GetRanges()[0]; RangeData data = new RangeData(); data.LoadData(this, rng, rng.Start, CutMode.CutImmediately); RangeData.ClipboardSetData(data); e.Handled = true; } //Delete else if (e.KeyCode == Keys.Delete && deleteEnabled) { ClearValues(selRegion); e.Handled = true; } #endregion }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); //verifico che l'eventuale edit sia terminato altrimenti esco if (Selection.ActivePosition.IsEmpty() == false) { CellContext focusCell = new CellContext(this, Selection.ActivePosition); if (focusCell.Cell != null && focusCell.IsEditing()) { if (focusCell.EndEdit(false) == false) return; } } //scateno eventi di MouseDown Position position = PositionAtPoint(new Point(e.X, e.Y)); if (position.IsEmpty() == false) { Cells.ICellVirtual cellMouseDown = GetCell(position); if (cellMouseDown != null) { ChangeMouseDownCell(position, position); //Cell.OnMouseDown CellContext cellContext = new CellContext(this, position, cellMouseDown); Controller.OnMouseDown(cellContext, e); } } else ChangeMouseDownCell(Position.Empty, Position.Empty); }
/// <summary> /// Process Delete, Ctrl+C, Ctrl+V, Up, Down, Left, Right, Tab keys /// </summary> /// <param name="e"></param> public virtual void ProcessSpecialGridKey(KeyEventArgs e) { if (e.Handled) return; bool enableArrows,enableTab,enablePageDownUp; enableArrows = enableTab = enablePageDownUp = false; if ( (SpecialKeys & GridSpecialKeys.Arrows) == GridSpecialKeys.Arrows) enableArrows = true; if ( (SpecialKeys & GridSpecialKeys.PageDownUp) == GridSpecialKeys.PageDownUp) enablePageDownUp = true; if ( (SpecialKeys & GridSpecialKeys.Tab) == GridSpecialKeys.Tab) enableTab = true; bool enableEscape = false; if ( (SpecialKeys & GridSpecialKeys.Escape) == GridSpecialKeys.Escape) enableEscape = true; bool enableEnter = false; if ( (SpecialKeys & GridSpecialKeys.Enter) == GridSpecialKeys.Enter) enableEnter = true; #region Processing keys //Escape if (e.KeyCode == Keys.Escape && enableEscape) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { if (focusCellContext.EndEdit(true)) e.Handled = true; } } //Enter if (e.KeyCode == Keys.Enter && enableEnter) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { focusCellContext.EndEdit(false); e.Handled = true; } } //Tab if (e.KeyCode == Keys.Tab && enableTab) { CellContext focusCellContext = new CellContext(this, Selection.ActivePosition); if (focusCellContext.Cell != null && focusCellContext.IsEditing()) { //se l'editing non riesce considero il tasto processato // altrimenti no, in questo modo il tab ha effetto anche per lo spostamento if (focusCellContext.EndEdit(false) == false) { e.Handled = true; return; } } } #endregion #region Navigate keys: arrows, tab and PgDown/Up var shiftPressed = e.Modifiers == Keys.Shift; var resetSelection = shiftPressed == false; if (e.KeyCode == Keys.Down && enableArrows) { Selection.MoveActiveCell(1, 0, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Up && enableArrows) { Selection.MoveActiveCell(-1, 0, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Right && enableArrows) { Selection.MoveActiveCell(0, 1, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Left && enableArrows) { Selection.MoveActiveCell(0, -1, resetSelection); e.Handled = true; } else if (e.KeyCode == Keys.Tab && enableTab) { //If the tab failed I automatically select the next control in the form (SelectNextControl) if (e.Modifiers == Keys.Shift) // backward { if (Selection.MoveActiveCell(0, -1, -1, int.MaxValue) == false) FindForm().SelectNextControl(this, false, true, true, true); e.Handled = true; } else //forward { if (Selection.MoveActiveCell(0, 1, 1, int.MinValue) == false) FindForm().SelectNextControl(this, true, true, true, true); e.Handled = true; } } else if ( (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown) && enablePageDownUp) { //MICK(18): I commented out this part - focusing is done within my CustomScrollPageDown() and CustomScrollPageUp() //Point focusPoint = PositionToRectangle(Selection.ActivePosition).Location; //focusPoint.Offset(1, 1); //in modo da entrare nella cella if (e.KeyCode == Keys.PageDown) CustomScrollPageDown(); else if (e.KeyCode == Keys.PageUp) CustomScrollPageUp(); //MICK(18): I commented out this part - focusing is done within my CustomScrollPageDown() and CustomScrollPageUp() //Position newPosition = PositionAtPoint(focusPoint); //if (Selection.CanReceiveFocus(newPosition)) // Selection.Focus(newPosition, true); e.Handled = true; } if (shiftPressed) { Selection.ResetSelection(true); Selection.SelectRange(new Range(m_firstCellShiftSelected, Selection.ActivePosition), true); } #endregion #region Clipboard RangeRegion selRegion = (ClipboardUseOnlyActivePosition ? new RangeRegion(Selection.ActivePosition) : Selection.GetSelectionRegion()); //Paste if (e.Control && e.KeyCode == Keys.V) { PerformPaste(selRegion); e.Handled = true; } //Copy else if (e.Control && e.KeyCode == Keys.C) { PerformCopy(selRegion); e.Handled = true; } //Cut else if (e.Control && e.KeyCode == Keys.X) { PerformCut(selRegion); e.Handled = true; } //Delete else if (e.KeyCode == Keys.Delete) { PerformDelete(selRegion); e.Handled = true; } #endregion }
/// <summary> /// Fired when a cell lost the focus /// </summary> /// <param name="e"></param> protected virtual void OnCellLostFocus(ChangeActivePositionEventArgs e) { if (e.Cancel) { return; } CellContext cellLostContext = new CellContext(Grid, e.OldFocusPosition); //Stop the Edit operation if (cellLostContext.EndEdit(false) == false) { e.Cancel = true; } if (e.Cancel) { return; } //evento Lost Focus if (CellLostFocus != null) { CellLostFocus(this, e); } if (e.Cancel) { return; } //Row/Column leaving //If the new Row is different from the current focus row calls a Row Leaving event int focusRow = ActivePosition.Row; if (ActivePosition.IsEmpty() == false && focusRow != e.NewFocusPosition.Row) { RowCancelEventArgs rowArgs = new RowCancelEventArgs(focusRow); OnFocusRowLeaving(rowArgs); if (rowArgs.Cancel) { e.Cancel = true; return; } } //If the new Row is different from the current focus row calls a Row Leaving event int focusColumn = ActivePosition.Column; if (ActivePosition.IsEmpty() == false && focusColumn != e.NewFocusPosition.Column) { ColumnCancelEventArgs columnArgs = new ColumnCancelEventArgs(focusColumn); OnFocusColumnLeaving(columnArgs); if (columnArgs.Cancel) { e.Cancel = true; return; } } //Change the focus cell to Empty m_ActivePosition = Position.Empty; //from now the cell doesn't have the focus //Cell Focus Left Grid.Controller.OnFocusLeft(new CellContext(Grid, e.OldFocusPosition), EventArgs.Empty); }
public override void OnDoubleClick(SourceGrid.CellContext sender, EventArgs e) { base.OnDoubleClick(sender, e); //MessageBox.Show(sender.Grid, sender.DisplayText); sender.EndEdit(true); }