Spread中本身内置了很多快捷键操作,不过有时候我们也需要实现一些自定义的快捷键行为,在Spread中只需继承FarPoint.Win.Spread.Action就可以实现自定义的快捷键。
本文简单介绍如何实现自定义的Delete键快捷行为,我们实现的功能是按下Delete时,只清空单元格的Value,不清空单元格的样式设置,如果单元格处于锁定状态不进行清空操作,代码实现如下:
public class ClearValueAction : FarPoint.Win.Spread.Action
{
public override void PerformAction(object source)
{
if (source is SpreadView)
{
SpreadView spread = (SpreadView)source;
SheetView sheet = spread.Sheets[spread.ActiveSheetIndex];
CellRange cr = sheet.GetSelection(0);
StyleInfo si = new StyleInfo();
for (int r = 0; r < cr.RowCount; r++)
{
for (int c = 0; c < cr.ColumnCount; c++)
{
sheet.Models.Style.GetCompositeInfo(cr.Row + r, cr.Column + c, -1, si);
if (!si.Locked)
{
sheet.Cells[cr.Row + r, cr.Column + c].ResetValue();
}
}
}
}
}
}
给Spread添加自定义的快捷键:
private void Form1_Load(object sender, EventArgs e)
{
InputMap im = fpSpread1.GetInputMap(InputMapMode.WhenFocused);
ActionMap am = fpSpread1.GetActionMap();
im.Put(new Keystroke(Keys.Delete, Keys.None), "ClearValue");
am.Put("ClearValue", new ClearValueAction());
}