カスタム コマンド ボタンの作成

カスタム コマンド ボタンの作成

Aspose.Cells.GridWeb でカスタム コマンド ボタンを作成するには:

  1. Aspose.Cells.GridWeb コントロールを Web フォームに追加します。
  2. ワークシートにアクセスします。
  3. CustomCommandButton クラスのインスタンスを作成します。
  4. ボタンのコマンドを何らかの値に設定します。この値は、ボタンのイベント ハンドラーで使用されます。
  5. ボタンのテキストを設定します。
  6. ボタンの画像 URL を設定します。
  7. 最後に、CustomCommandButton オブジェクトを GridWeb コントロールの CustomCommandButtons コレクションに追加します。

コード スニペットの出力を以下に示します。

GridWeb コントロールに追加されたカスタム コマンド ボタン

todo:画像_代替_文章

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Instantiating a CustomCommandButton object
CustomCommandButton button = new CustomCommandButton();
// Setting the command, text and image URL for button. Image should be relative to website root folder
button.Command = "MyButton";
button.Text = "MyButton";
button.ImageUrl = "../Image/aspose.ico";
// Adding button to CustomCommandButtons collection of GridWeb
GridWeb1.CustomCommandButtons.Add(button);

カスタム コマンド ボタンのイベント処理

カスタム コマンド ボタンの最も重要な側面は、クリックしたときに実行されるアクションです。アクションを設定するには、GridWeb コントロールの CustomCommand イベントのイベント ハンドラーを作成します。

CustomCommand イベントは、カスタム コマンド ボタンがクリックされると常にトリガーされます。そのため、イベント ハンドラーは、ボタンを GridWeb コントロールに追加するときに、コマンド セットによって適用される特定のカスタム コマンド ボタンを識別する必要があります。最後に、ボタンがクリックされたときに実行されるカスタム コードを追加します。

以下のコード例では、ボタンがクリックされると、セル A1 にテキスト メッセージが追加されます。

カスタム コマンド ボタンがクリックされたときに A1 セルに追加されるテキスト

todo:画像_代替_文章

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Creating Event Handler for CustomCommand event
protected void GridWeb1_CustomCommand(object sender, string command)
{
// Identifying a specific button by checking its command
if (command.Equals("MyButton"))
{
// Accessing the cells collection of the worksheet that is currently active
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex];
// Putting value to "A1" cell
sheet.Cells["A1"].PutValue("My Custom Command Button is Clicked.");
// Set first column width to make the text visible
sheet.Cells.SetColumnWidth(0, 30);
}
}