ワークシートにセルコントロールを追加する
紹介
現在、Aspose.Cells.GridDesktopは次の3つのタイプのセルコントロールをサポートしています。
- ボタン
- チェックボックス
- ComboBox
これらのコントロールはすべて、抽象クラスCellControlから派生しています。 各ワークシートにはControlsのコレクションが含まれています。 新しいセルコントロールを追加し、既存のものに簡単にアクセスするためには、このControlsコレクションを使用できます。
重要: 1つずつ追加する代わりに、列のすべてのセルにセルコントロールを追加したい場合は、列の中のセルコントロールの管理を参照してください。
ボタンの追加
Aspose.Cells.GridDesktopを使用してワークシートにボタンを追加するには、以下の手順に従ってください:
- あなたのフォームにAspose.Cells.GridDesktopコントロールを追加
- 好きなワークシートにアクセス
- ワークシートのControlsコレクションにボタンを追加
ボタンを追加する際には、セルの場所(表示する位置)、幅と高さ、およびボタンのキャプションを指定できます。
ボタンのイベント処理
私たちはワークシートにボタンコントロールを追加したところで、ボタンがクリックされても使用しない場合の利点は何でしょうか。 ですから、ここでボタンのイベント処理の必要性が出てきます。
ボタンコントロールのClickイベントを処理するには、Aspose.Cells.GridDesktopはCellButtonClickイベントを提供しており、開発者はそれを自分の必要に応じて実装する必要があります。 たとえば、以下にボタンがクリックされたときにメッセージを表示しただけの例があります:
ボタンコントロールの背景画像の指定
ボタンコントロールのラベル/テキストとともに背景画像/写真を設定することができます。 以下のコードに示すように。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Set the image. | |
Image image = Image.FromFile(dataDir + @"AsposeLogo.jpg"); | |
button.Image = image; |
重要: セルコントロールのすべてのイベントには、そのイベントがトリガーされたセルコントロールを含むセルの行番号と列番号を提供するCellControlEventArgs引数が含まれています。
チェックボックスの追加
Aspose.Cells.GridDesktopを使用してワークシートにチェックボックスを追加する場合は、以下の手順に従ってください。
- あなたのフォームにAspose.Cells.GridDesktopコントロールを追加
- 好きなワークシートにアクセス
- ワークシートのControlsコレクションにチェックボックスを追加
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Accessing the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Adding checkbox to the Controls collection of the Worksheet | |
sheet.Controls.AddCheckBox(cl.Row, cl.Column, true); |
チェックボックスを追加する際には、セルの場所(表示する位置)とチェックボックスの状態を指定できます。
チェックボックスのイベント処理
Aspose.Cells.GridDesktopは、チェックボックスのChecked状態が変更されたときにトリガーされるCellCheckedChangedイベントを提供しています。 開発者は必要に応じてこのイベントを処理できます。 たとえば、以下にチェックボックスのChecked状態を表示するためのメッセージを表示しただけの例があります:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Implenting CellCheckedChanged event handler | |
private void gridDesktop1_CellCheckedChanged(object sender, CellControlEventArgs e) | |
{ | |
// Getting the reference of the CheckBox control whose event is triggered | |
Aspose.Cells.GridDesktop.CheckBox check = (Aspose.Cells.GridDesktop.CheckBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column]; | |
// Displaying the message when the Checked state of CheckBox is changed | |
MessageBox.Show("Current state of CheckBox is " + check.Checked); | |
} |
コンボボックスの追加
Aspose.Cells.GridDesktopを使用してワークシートにコンボボックスを追加するには、以下の手順に従ってください。
- あなたのフォームにAspose.Cells.GridDesktopコントロールを追加
- 好きなワークシートにアクセス
- ComboBoxがクリックされたときに表示されるアイテム(または値)の配列を作成
- セルの場所(コンボボックスが表示される場所)と、コンボボックスがクリックされたときに表示されるアイテム/値を指定してワークシートのControlsコレクションにComboBoxを追加
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the worksheet of the Grid that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Accessing the location of the cell that is currently in focus | |
CellLocation cl = sheet.GetFocusedCellLocation(); | |
// Creating an array of items or values that will be added to combobox | |
string[] items = new string[3]; | |
items[0] = "Aspose"; | |
items[1] = "Aspose.Grid"; | |
items[2] = "Aspose.Grid.Desktop"; | |
// Adding combobox to the Controls collection of the Worksheet | |
sheet.Controls.AddComboBox(cl.Row, cl.Column, items); |
コンボボックスのイベント処理
Aspose.Cells.GridDesktopは、CellSelectedIndexChanged イベントを提供しています。このイベントは、コンボボックスの選択されたインデックスが変更されたときにトリガーされます。開発者はこのイベントを自分の望むように処理できます。たとえば、コンボボックスの選択されたアイテムを表示するためのメッセージを表示するだけであるとします。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Implenting CellSelectedIndexChanged event handler | |
private void gridDesktop1_CellSelectedIndexChanged(object sender, CellComboBoxEventArgs e) | |
{ | |
// Getting the reference of the ComboBox control whose event is triggered | |
Aspose.Cells.GridDesktop.ComboBox combo = | |
(Aspose.Cells.GridDesktop.ComboBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column]; | |
// Displaying the message when the Selected Index of ComboBox is changed | |
MessageBox.Show(combo.Items[combo.SelectedIndex].ToString()); | |
} |