using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Model;

namespace Spread_WF_Delete_Content
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //方法一
            // this.fpSpread1.KeyDown += new KeyEventHandler(spread_KeyDown);

            //方法二
            this.fpSpread1.GetInputMap(InputMapMode.WhenFocused).Put(new Keystroke(Keys.Delete, Keys.None), FarPoint.Win.Spread.SpreadActions.ClearSelectedCells);
            this.fpSpread1.GetActionMap().Put(SpreadActions.ClearSelectedCells, new ClearSelectedCellsUndoAction());
        }


        private void spread_KeyDown(object source, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                FpSpread spread = (FpSpread)source;
                int selectionCount = spread.ActiveSheet.SelectionCount;
                if (selectionCount > 0)
                {
                    for (int i = 0; i < selectionCount; i++)
                    {
                        CellRange range = spread.ActiveSheet.GetSelection(i);
                        spread.ActiveSheet.ClearRange(range.Row, range.Column, range.RowCount, range.ColumnCount, true);
                    }
                }
                else
                {
                    int activeRow = spread.ActiveSheet.ActiveRowIndex;
                    int activeColumn = spread.ActiveSheet.ActiveColumnIndex;
                    spread.ActiveSheet.ClearRange(activeRow, activeColumn, 1, 1, true);
                }
                e.Handled = true;
            }
        }
    }
}
