SPREAD for Windows Forms 8.0J サンプルコード集
独自のアクションマップを作成する(既存クラスの継承)

SpreadActionsクラスより提供されている入力マップ用の各フィールド以外にも、Actionクラスを継承したサブクラスを作成することで独自のアクションを作成することができます。本サンプルでは、[F8]キー押下によってアクティブセルの背景色を変更する例を紹介します。

  • 既存クラスの継承は.NETアプリケーション開発における一般的な手法です。継承方法の詳細についてはMSDNライブラリをご参照ください。
  • 本サンプルは基本的な継承例です。実際にはプロジェクトの仕様に基づいた各カスタマイズが必要となります。
  • サブクラス内に定義できるのは一つの動作のみです。そのため、複数のアクションが必要な場合にはそれぞれのサブクラスを作成する必要があります。
  • 入力マップには2種類の適用条件があります。
    1. WhenFocused(コントロールにフォーカスがある場合:セル非編集モード)
    2. WhenAncestorOfFocused(コントロールまたはその子にフォーカスがある場合:セル編集モード)
  • SPREADでは対象キー押下によって行われる動作をあらかじめ入力マップにてデフォルト定義(SpreadActionsクラスメンバを利用)しており、キー押下によってその動作を実施します。これは「定義した動作をSPREADに行わせる」という意味合いとなるため、例えば[F2]キー押下によって行われる動作(SpreadActions.ClearCell:アクティブセルの内容をクリア)を任意のイベント等から利用することはできません。

 private void Form1_Load(object sender, System.EventArgs e)
 {

   FarPoint.Win.Spread.InputMap im = new FarPoint.Win.Spread.InputMap();
   im = fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused);
   FarPoint.Win.Spread.ActionMap am = new FarPoint.Win.Spread.ActionMap();
   am = fpSpread1.GetActionMap();

   //作成したサブクラスを[F8]キーにマッピングします
   im.Put(new FarPoint.Win.Spread.Keystroke(Keys.F8, Keys.None), "ColorAction");
   am.Put("ColorAction", new ColorAction());

 }
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

   Dim im As New FarPoint.Win.Spread.InputMap
   im = FpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)
   Dim am As New FarPoint.Win.Spread.ActionMap
   am = FpSpread1.GetActionMap()

   '作成したサブクラスを[F8]キーにマッピングします
   im.Put(New FarPoint.Win.Spread.Keystroke(Keys.F8, Keys.None), "ColorAction")
   am.Put("ColorAction", New ColorAction)

 End Sub

カスタムアクションクラス

 public class ColorAction : FarPoint.Win.Spread.Action //Actionクラスを継承したサブクラスを作成します
 {

   public override void PerformAction(object sender)
   {
      if (sender is FarPoint.Win.Spread.SpreadView)
      {
         //アクティブシートを取得します
         FarPoint.Win.Spread.SpreadView spread = (FarPoint.Win.Spread.SpreadView)sender;
         FarPoint.Win.Spread.SheetView sheet = spread.Sheets[spread.ActiveSheetIndex];
         //アクティブセルの背景色を設定します
         sheet.Cells[sheet.ActiveRowIndex, sheet.ActiveColumnIndex].BackColor = Color.Red;
      }
   }

 }
 Public Class ColorAction

   'Actionクラスを継承したサブクラスを作成します
   Inherits FarPoint.Win.Spread.Action

   Public Overrides Sub PerformAction(ByVal sender As Object)

      If TypeOf sender Is FarPoint.Win.Spread.SpreadView Then
         'アクティブシートを取得します
         Dim spread As FarPoint.Win.Spread.SpreadView = CType(sender, FarPoint.Win.Spread.SpreadView)
         Dim sheet As FarPoint.Win.Spread.SheetView = spread.Sheets(spread.ActiveSheetIndex)
         'アクティブセルの背景色を設定します
         sheet.Cells(sheet.ActiveRowIndex, sheet.ActiveColumnIndex).BackColor = Color.Red
      End If

   End Sub

 End Class

 

 


© 2004-2015, GrapeCity inc. All rights reserved.