MESCIUS SPREAD for Windows Forms 17.0J サンプルコード集
アイテム選択時の動作を変更する

リボンコントロールのボタンはコマンドという形で管理されており、ボタンの押下時はコマンドが実行されます。実行されるコマンドはCommandExecutingイベント/CommandExecutedイベントで取得することができ、これらのイベントを使用して組み込みコマンドをキャンセルすることやオーバーライドすることができます。以下のサンプルでは切り取りコマンドをオーバーライドし切り取られたセル範囲の背景色を設定しています。リボンコントロールの[ホーム]-[クリップボード]-[切り取り]またはコンテキストメニューの[切り取り]を選択することで動作を確認できます。

【実行例】

private void Form1_Load(object sender, EventArgs e)
 {
  // リボンコントロールとSPREADコントロールを関連付けます
  ribbonBar1.Attach(fpSpread1);

  // テストデータを設定します
  fpSpread1.AsWorkbook().ActiveSheet.Cells[0, 0].Value = "名前";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[0, 1].Value = "性別";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[0, 2].Value = "報酬";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[0, 3].Value = "職業";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[1, 0].Value = "佐藤";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[1, 1].Value = "男";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[1, 2].Value = 300000;
  fpSpread1.AsWorkbook().ActiveSheet.Cells[1, 3].Value = "教師";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[2, 0].Value = "高橋";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[2, 1].Value = "男";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[2, 2].Value = 300000;
  fpSpread1.AsWorkbook().ActiveSheet.Cells[2, 3].Value = "エンジニア";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[3, 0].Value = "木村";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[3, 1].Value = "女";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[3, 2].Value = 320000;
  fpSpread1.AsWorkbook().ActiveSheet.Cells[3, 3].Value = "教師";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[4, 0].Value = "高山";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[4, 1].Value = "女";
  fpSpread1.AsWorkbook().ActiveSheet.Cells[4, 2].Value = 270000;
  fpSpread1.AsWorkbook().ActiveSheet.Cells[4, 3].Value = "会計士";

  ribbonBar1.CommandExecuting += RibbonBar1_CommandExecuting;
  ribbonBar1.CommandExecuted += RibbonBar1_CommandExecuted;
 }

 private void RibbonBar1_CommandExecuting(object sender, ExecuteCommandEventArgs e)
 {
  if (e.CommandName == "Cut")
  {
   // デフォルトの動作をキャンセルする場合は「e.Handled = true」
   //e.Handled = true;
  }
 }

 private void RibbonBar1_CommandExecuted(object sender, ExecuteCommandEventArgs e)
 {
  if (e.CommandName == "Cut")
  {
   // 切り取られたセル範囲に灰色を設定します
   GrapeCity.Spreadsheet.Color color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Gray);
   fpSpread1.AsWorkbook().ActiveSheet.Selection.Interior.Color = color;
  }
 }
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  ' リボンコントロールとSPREADコントロールを関連付けます
  RibbonBar1.Attach(FpSpread1)

  ' テストデータを設定します
  FpSpread1.AsWorkbook().ActiveSheet.Cells(0, 0).Value = "名前"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(0, 1).Value = "性別"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(0, 2).Value = "報酬"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(0, 3).Value = "職業"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(1, 0).Value = "佐藤"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(1, 1).Value = "男"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(1, 2).Value = 300000
  FpSpread1.AsWorkbook().ActiveSheet.Cells(1, 3).Value = "教師"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(2, 0).Value = "高橋"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(2, 1).Value = "男"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(2, 2).Value = 300000
  FpSpread1.AsWorkbook().ActiveSheet.Cells(2, 3).Value = "エンジニア"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(3, 0).Value = "木村"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(3, 1).Value = "女"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(3, 2).Value = 320000
  FpSpread1.AsWorkbook().ActiveSheet.Cells(3, 3).Value = "教師"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(4, 0).Value = "高山"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(4, 1).Value = "女"
  FpSpread1.AsWorkbook().ActiveSheet.Cells(4, 2).Value = 270000
  FpSpread1.AsWorkbook().ActiveSheet.Cells(4, 3).Value = "会計士"
 End Sub

 Private Sub RibbonBar1_CommandExecuting(ByVal sender As Object, ByVal e As ExecuteCommandEventArgs) Handles RibbonBar1.CommandExecuting
  If e.CommandName = "Cut" Then
   ' デフォルトの動作をキャンセルする場合は「e.Handled = true」
   'e.Handled = True
  End If
 End Sub

 Private Sub RibbonBar1_CommandExecuted(sender As Object, e As ExecuteCommandEventArgs) Handles RibbonBar1.CommandExecuted
  If e.CommandName = "Cut" Then
   ' 切り取られたセル範囲に灰色を設定します
   Dim color As GrapeCity.Spreadsheet.Color = GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Gray)
   FpSpread1.AsWorkbook().ActiveSheet.Selection.Interior.Color = color
  End If
 End Sub

 

 


© MESCIUS inc. All rights reserved.