作成日: 2023/02/06 最終更新日: 2023/02/06
文書種別
技術情報
詳細
独自のアクションを作成してキーマップに割り当てることで、キー操作によるアクティブセルの移動をカスタマイズすることができます。
以下の例では、Tabキー押下によりアクティブセルを奇数行では1,3,5列目、偶数行では2,4列目のセルに移動させます。
以下の例では、Tabキー押下によりアクティブセルを奇数行では1,3,5列目、偶数行では2,4列目のセルに移動させます。
◎サンプルコード(VB)
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' シートの設定
FpSpread1.ActiveSheet.ColumnCount = 5
' 入力マップの設定
Dim im = FpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused)
im.Put(New FarPoint.Win.Spread.Keystroke(Keys.Tab, Keys.None), "CustomMove")
' アクションマップの設定
Dim am = FpSpread1.GetActionMap()
am.Put("CustomMove", New CustomMove())
End Sub
End Class
' 独自のアクション
Public Class CustomMove
Inherits FarPoint.Win.Spread.Action
Public Overrides Sub PerformAction(sender As Object)
Dim sv = DirectCast(sender, FarPoint.Win.Spread.SpreadView)
Dim sheet = sv.GetSheetView()
If sheet.ActiveRowIndex Mod 2 = 0 Then
' 奇数行(行番号は偶数)
If sheet.ActiveColumnIndex = 0 OrElse sheet.ActiveColumnIndex = 2 Then
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
ElseIf sheet.ActiveColumnIndex = sheet.ColumnCount - 1 Then
' 最終列のとき
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextRowFirstColumn).PerformAction(sv)
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
Else
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
End If
Else
' 偶数行(行番号は奇数)
If sheet.ActiveColumnIndex = 1 Then
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
ElseIf sheet.ActiveColumnIndex = sheet.ColumnCount - 2 Then
' 最終列の1列前のとき
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextRowFirstColumn).PerformAction(sv)
Else
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv)
End If
End If
End Sub
End Class
◎サンプルコード(C#)
private void Form1_Load(object sender, EventArgs e)
{
// シートの設定
fpSpread1.ActiveSheet.ColumnCount = 5;
// 入力マップの設定
var im = fpSpread1.GetInputMap(FarPoint.Win.Spread.InputMapMode.WhenFocused);
im.Put(new FarPoint.Win.Spread.Keystroke(Keys.Tab, Keys.None), "CustomMove");
// アクションマップの設定
var am = fpSpread1.GetActionMap();
am.Put("CustomMove", new CustomMove());
}
public class CustomMove : FarPoint.Win.Spread.Action
{
public override void PerformAction(object sender)
{
var sv = (FarPoint.Win.Spread.SpreadView)sender;
var sheet = sv.GetSheetView();
if (sheet.ActiveRowIndex % 2 == 0)
{
// 奇数行(行番号は偶数)
if (sheet.ActiveColumnIndex == 0 || sheet.ActiveColumnIndex == 2)
{
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
}
else if (sheet.ActiveColumnIndex == sheet.ColumnCount - 1)
{
// 最終列のとき
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextRowFirstColumn).PerformAction(sv);
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
}
else
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
}
else
// 偶数行(行番号は奇数)
if (sheet.ActiveColumnIndex == 1)
{
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
}
else if (sheet.ActiveColumnIndex == sheet.ColumnCount - 2)
// 最終列の1列前のとき
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextRowFirstColumn).PerformAction(sv);
else
sv.GetActionMap().Get(FarPoint.Win.Spread.SpreadActions.MoveToNextColumn).PerformAction(sv);
}
}
<実行イメージ>
なお、上の設定ではTabキー操作によるアクティブセルの移動を制御しているだけで、矢印キーやマウスクリックなどですべてのセルはアクティブにすることができます。