Spread Winform中响应delete按键实现数据删除,需要手写代码。 在实践的过程发现有3种办法可供选择:
- 响应KeyDown事件
- 自定义Action
- 自定义UndoAction
- 最终效果见截图:
比如我是选中A4:B5 的8个单元格,在按delete键,按照excel的话,直接就删除八个里的数据
响应KeyDown事件
判断当前按键为delete键,对selection内容ActiveSheet.ClearRange即可,代码如下所示:
1: this.fpSpread1.KeyDown += new KeyEventHandler(spread_KeyDown);
2:
3: private void spread_KeyDown(object source, KeyEventArgs e)
4: {
5: if (e.KeyCode == Keys.Delete)
6: {
7: FpSpread spread = (FpSpread)source;
8: int selectionCount = spread.ActiveSheet.SelectionCount;
9: if (selectionCount > 0)
10: {
11: for (int i = 0; i < selectionCount; i++)
12: {
13: CellRange range = spread.ActiveSheet.GetSelection(i);
14: spread.ActiveSheet.ClearRange(range.Row, range.Column, range.RowCount, range.ColumnCount, true);
15: }
16: }
17: else
18: {
19: int activeRow = spread.ActiveSheet.ActiveRowIndex;
20: int activeColumn = spread.ActiveSheet.ActiveColumnIndex;
21: spread.ActiveSheet.ClearRange(activeRow, activeColumn, 1, 1, true);
22: }
23: e.Handled = true;
24: }
25: }
26:
自定义Action
前面有一篇博文已经针对专门写的,且有C#、VB的源码:
自定义UndoAction
当有了删除操作,就会有Undo的潜在需求,玩意误删除怎么办呢? 毕竟Spread的UI和Excel非常相似。实现一个Undo、Redo操作在Spread下也非常简单。
需要一个继承UndoAction的类,每次撤销、重做算一次完整的操作,会实例化一个UndoAction继承类。
源码如下所示:
1: this.fpSpread1.GetInputMap(InputMapMode.WhenFocused).Put(new Keystroke(Keys.Delete, Keys.None), FarPoint.Win.Spread.SpreadActions.ClearSelectedCells);
2: this.fpSpread1.GetActionMap().Put(SpreadActions.ClearSelectedCells, new ClearSelectedCellsUndoAction());
3:
4:
5: public class ClearSelectedCellsUndoAction : FarPoint.Win.Spread.UndoRedo.UndoAction
6: {
7: SpreadView spreadView = null; // SpreadView where action happens
8: FarPoint.Win.Spread.SheetView activeSheet = null; // active SheetView in root workbook
9: FarPoint.Win.Spread.SheetView sheet = null; // SheetView where action happens
10: FarPoint.Win.Spread.Model.CellRange cellRange = null; // CellRange being cleared
11: DataObject clipData = null; // DataObject containing data from CellRange
12:
13: public override bool PerformUndoAction(object sender)
14: {
15: SpreadView rootWorkbook = sender as SpreadView; // sender is always root SpreadView when called from UI
16:
17: if (rootWorkbook != null) // but sender might be null if other code calls this, so check!
18: {
19: activeSheet = rootWorkbook.Sheets[rootWorkbook.ActiveSheetIndex]; // save active SheetView (to restore on undo)
20: spreadView = rootWorkbook.GetActiveWorkbook(); // save active SpreadView (to restore on undo)
21: sheet = spreadView.GetSheetView(); // get SheetView to operate on
22: cellRange = sheet.GetSelection(sheet.SelectionCount - 1); // get CellRange to operate on
23: if (cellRange == null) // GetSelection return null if there is no selection, just active cell
24: cellRange = new CellRange(sheet.ActiveRowIndex, sheet.ActiveColumnIndex, 1, 1); // use active cell in this case
25: }
26: if (SaveUndoState()) // save data from range
27: { // then clear it
28: sheet.ClearRange(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount, false);
29: return true;
30: }
31: return false; // something failed, return false to discard action from undo stack
32: }
33:
34: protected override bool SaveUndoState()
35: {
36: if (cellRange != null) // need CellRange set in PerformUndoAction (implied sheet is valid)
37: clipData = sheet.GetClipDataObject(false, cellRange, ClipboardCopyOptions.All); // save data object for cell range
38: return clipData != null;
39: }
40:
41: public override bool Undo(object sender)
42: {
43: SpreadView rootWorkbook = sender as SpreadView;
44: if (rootWorkbook != null)
45: {
46: rootWorkbook.ActiveSheetIndex = rootWorkbook.Sheets.IndexOf(activeSheet); // restore active sheet (in case user changed it)
47: rootWorkbook.SetActiveWorkbook(spreadView); // restore active workbook in sheet (in case user changed it)
48: sheet.ClearSelection(); // reset selection (clear, then add)
49: sheet.AddSelection(cellRange.Row, cellRange.Column, cellRange.RowCount, cellRange.ColumnCount);
50: sheet.ClipboardPaste(ClipboardPasteOptions.All, clipData); // paste data pack from data object
51: return true;
52: }
53: return false;
54: }
55: }
56:
C#源码如下: