選択範囲のデータを[Delete]キー押下によって一括削除したい

文書番号 : 31272     文書種別 : 使用方法     最終更新日 : 2011/08/19
文書を印刷する
対象製品
SPREAD for Windows Forms 5.0J
詳細
指定範囲のデータのみをクリアする場合には、[Delete]キーに割り当てられているデフォルトの入力マップをClearSelectedCells(選択されたセルの内容をすべて削除)に変更することで実現できます。

◎サンプルコード(VB)
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FpSpread1.ActiveSheet.RowCount = 10
    FpSpread1.ActiveSheet.ColumnCount = 5
    Dim i As Integer
    Dim j As Integer
    For i = 0 To FpSpread1.ActiveSheet.RowCount - 1
      For j = 0 To FpSpread1.ActiveSheet.ColumnCount - 1
        FpSpread1.ActiveSheet.SetValue(i, j, i + j)
      Next
    Next

    '非編集セルでの[Delete]キーを「ClearSelectedCells(選択されたセルの内容をすべて削除)」とします
    Dim im As New FarPoint.Win.Spread.InputMap
    im = FpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)
    im.Put(New FarPoint.Win.Spread.Keystroke(Keys.Delete, Keys.None), FarPoint.Win.Spread.SpreadActions.ClearSelectedCells)
  End Sub


◎サンプルコード(C#)
  private void Form1_Load(object sender, EventArgs e)
  {
    fpSpread1.ActiveSheet.RowCount = 10;
    fpSpread1.ActiveSheet.ColumnCount = 5;
    for (int i = 0; i < fpSpread1.Sheets[0].RowCount; i++)
    {
      for (int j = 0; j < fpSpread1.Sheets[0].ColumnCount; j++)
      {
        fpSpread1.ActiveSheet.SetValue(i, j, i + j);
      }
    }

    //非編集セルでの[Delete]キーを「ClearSelectedCells(選択されたセルの内容をすべて削除)」とします
    FarPoint.Win.Spread.InputMap im = new FarPoint.Win.Spread.InputMap();
    im = fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused);
    im.Put(new FarPoint.Win.Spread.Keystroke(Keys.Delete, Keys.None), FarPoint.Win.Spread.SpreadActions.ClearSelectedCells);
  }


なお、ClearSelectedCellsフィールドはセルの値だけではなく書式も削除する仕様となります。選択範囲のセルの値だけを削除するには、独自のアクションを作成する方法をご検討ください。

◎サンプルコード(VB)
Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    FpSpread1.ActiveSheet.RowCount = 10
    FpSpread1.ActiveSheet.ColumnCount = 5
    Dim i As Integer
    Dim j As Integer
    For i = 0 To FpSpread1.ActiveSheet.RowCount - 1
      For j = 0 To FpSpread1.ActiveSheet.ColumnCount - 1
        FpSpread1.ActiveSheet.SetValue(i, j, i + j)
      Next
    Next

    ' 独自のアクションの設定
    Dim am As FarPoint.Win.Spread.ActionMap = FpSpread1.GetActionMap()
    am.Put("CustomAction", New CustomAction())

    ' 入力マップの設定
    Dim im As FarPoint.Win.Spread.InputMap = FpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)
    im.Put(New FarPoint.Win.Spread.Keystroke(Keys.Delete, Keys.None), "CustomAction")
  End Sub
End Class

Public Class CustomAction
  Inherits FarPoint.Win.Spread.Action

  Public Overrides Sub PerformAction(ByVal sender As Object)
    ' SPREADの取得
    Dim spread As FarPoint.Win.Spread.FpSpread = CType(sender, FarPoint.Win.Spread.SpreadView).Owner
    ' 選択範囲の取得
    Dim range As FarPoint.Win.Spread.Model.CellRange = spread.ActiveSheet.GetSelection(0)
    ' 選択範囲のセルの値のクリア
    spread.ActiveSheet.ClearRange(range.Row, range.Column, range.RowCount, range.ColumnCount, True)
  End Sub
End Class


◎サンプルコード(C#)
public partial class Form1 : Form
{
  private void Form1_Load(object sender, EventArgs e)
  {
    fpSpread1.ActiveSheet.RowCount = 10;
    fpSpread1.ActiveSheet.ColumnCount = 5;
    for (int i = 0; i < fpSpread1.Sheets[0].RowCount; i++)
    {
      for (int j = 0; j < fpSpread1.Sheets[0].ColumnCount; j++)
      {
        fpSpread1.ActiveSheet.SetValue(i, j, i + j);
      }
    }

    // 独自のアクションの設定
    FarPoint.Win.Spread.ActionMap am = fpSpread1.GetActionMap();
    am.Put("CustomAction", new CustomAction());

    // 入力マップの設定
    FarPoint.Win.Spread.InputMap im = new FarPoint.Win.Spread.InputMap();
    im = fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused);
    im.Put(new FarPoint.Win.Spread.Keystroke(Keys.Delete, Keys.None), "CustomAction");
  }
}

public class CustomAction : FarPoint.Win.Spread.Action
{
  public override void PerformAction(object sender)
  {
    // SPREADの取得
    FarPoint.Win.Spread.SpreadView spread = (FarPoint.Win.Spread.SpreadView)sender;
    FarPoint.Win.Spread.SheetView sheet = spread.Sheets[spread.ActiveSheetIndex];

    // 選択範囲の取得
    FarPoint.Win.Spread.Model.CellRange range = sheet.GetSelection(0);
    // 選択範囲のセルの値のクリア
    sheet.ClearRange(range.Row, range.Column, range.RowCount, range.ColumnCount, true);
  }
}
関連情報
キーワード
「キー操作」