MESCIUS SPREAD for Windows Forms 17.0J サンプルコード集 > リボンコントロール > 新しいタブ、グループ、アイテムを追加する |
リボンコントロールにはRibbonTabクラス、RibbonGroupクラス、RibbonItemクラスが用意されており、これらのクラスを使用することで新しいタブ、グループ、アイテムを追加することが可能です。独自に追加したアイテムは組み込みのコマンドと同じようにCommandExecuting/CommandExecutedイベントで実装できます。以下のサンプルではCommandExecutingイベントを使用してアクティブセルの位置からテストデータを挿入しています。
private void Form1_Load(object sender, EventArgs e) { // リボンコントロールとSPREADコントロールを関連付けます ribbonBar1.Attach(fpSpread1); // リボンタブを挿入します ribbonBar1.Tabs.Insert(0, new GrapeCity.Spreadsheet.WinForms.Ribbon.RibbonTab()); ribbonBar1.Tabs[0].Text = "新しいタブ"; ribbonBar1.Tabs[0].Groups.Add(new GrapeCity.Spreadsheet.WinForms.Ribbon.RibbonGroup()); ribbonBar1.Tabs[0].Groups[0].Text = "新しいグループ"; ribbonBar1.Tabs[0].Groups[0].Items.Add("サンプルデータ"); ribbonBar1.Tabs[0].Groups[0].Items[0].CommandName = "SampleData"; ribbonBar1.CommandExecuting += RibbonBar1_CommandExecuting; ribbonBar1.CommandExecuted += RibbonBar1_CommandExecuted; } private void RibbonBar1_CommandExecuting(object sender, ExecuteCommandEventArgs e) { if (e.CommandName == "SampleData") { // データを設定します int row = fpSpread1.AsWorkbook().ActiveSheet.ActiveCell.Row; int col = fpSpread1.AsWorkbook().ActiveSheet.ActiveCell.Column; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 0, col + 0].Value = "名前"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 0, col + 1].Value = "性別"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 0, col + 2].Value = "報酬"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 0, col + 3].Value = "職業"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 1, col + 0].Value = "佐藤"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 1, col + 1].Value = "男"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 1, col + 2].Value = 300000; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 1, col + 3].Value = "教師"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 2, col + 0].Value = "高橋"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 2, col + 1].Value = "男"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 2, col + 2].Value = 300000; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 2, col + 3].Value = "エンジニア"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 3, col + 0].Value = "木村"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 3, col + 1].Value = "女"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 3, col + 2].Value = 320000; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 3, col + 3].Value = "教師"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 4, col + 0].Value = "高山"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 4, col + 1].Value = "女"; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 4, col + 2].Value = 270000; fpSpread1.AsWorkbook().ActiveSheet.Cells[row + 4, col + 3].Value = "会計士"; // コマンドが実行されたことを通知します e.Handled = true; } }
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' リボンコントロールとSPREADコントロールを関連付けます RibbonBar1.Attach(FpSpread1) ' リボンタブを挿入します RibbonBar1.Tabs.Insert(0, New GrapeCity.Spreadsheet.WinForms.Ribbon.RibbonTab()) RibbonBar1.Tabs(0).Text = "新しいタブ" RibbonBar1.Tabs(0).Groups.Add(New GrapeCity.Spreadsheet.WinForms.Ribbon.RibbonGroup()) RibbonBar1.Tabs(0).Groups(0).Text = "新しいグループ" RibbonBar1.Tabs(0).Groups(0).Items.Add("サンプルデータ") RibbonBar1.Tabs(0).Groups(0).Items(0).CommandName = "SampleData" End Sub Private Sub RibbonBar1_CommandExecuting(ByVal sender As Object, ByVal e As ExecuteCommandEventArgs) Handles RibbonBar1.CommandExecuting If e.CommandName = "SampleData" Then ' データを設定します Dim row As Integer = FpSpread1.AsWorkbook().ActiveSheet.ActiveCell.Row Dim col As Integer = FpSpread1.AsWorkbook().ActiveSheet.ActiveCell.Column FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 0, col + 0).Value = "名前" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 0, col + 1).Value = "性別" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 0, col + 2).Value = "報酬" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 0, col + 3).Value = "職業" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 1, col + 0).Value = "佐藤" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 1, col + 1).Value = "男" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 1, col + 2).Value = 300000 FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 1, col + 3).Value = "教師" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 2, col + 0).Value = "高橋" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 2, col + 1).Value = "男" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 2, col + 2).Value = 300000 FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 2, col + 3).Value = "エンジニア" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 3, col + 0).Value = "木村" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 3, col + 1).Value = "女" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 3, col + 2).Value = 320000 FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 3, col + 3).Value = "教師" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 4, col + 0).Value = "高山" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 4, col + 1).Value = "女" FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 4, col + 2).Value = 270000 FpSpread1.AsWorkbook().ActiveSheet.Cells(row + 4, col + 3).Value = "会計士" ' コマンドが実行されたことを通知します e.Handled = True End If End Sub