作成日: 2019/07/25 最終更新日: 2019/07/25
文書種別
使用方法
詳細
ContextMenuStripクラスを使用して、コンテキストメニューを表示しての行追加・削除が可能です。
◎サンプルコード(VB)
◎サンプルコード(C#)
◎サンプルコード(VB)
Public Class Form1
Private customMenu As ContextMenuStrip
Private currentRow As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' コンテキストメニューの作成
customMenu = New ContextMenuStrip()
customMenu.Items.Add("行の挿入")
customMenu.Items.Add("行の削除")
AddHandler customMenu.ItemClicked, AddressOf menu_ItemClicked
' SPREADの設定
For i As Integer = 0 To FpSpread1.ActiveSheet.RowCount - 1
FpSpread1.ActiveSheet.SetValue(i, 0, i)
Next
End Sub
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
' コンテキストメニューのクローズ
DirectCast(sender, ContextMenuStrip).Close()
' 処理の実行
Select Case e.ClickedItem.Text
Case "行の削除"
FpSpread1.ActiveSheet.RemoveRows(currentRow, 1)
Case "行の挿入"
FpSpread1.ActiveSheet.AddRows(currentRow, 1)
End Select
End Sub
Private Sub FpSpread1_MouseDown(sender As Object, e As MouseEventArgs) Handles FpSpread1.MouseDown
' マウスの右ボタンが押下された場合
If e.Button = MouseButtons.Right Then
' マウスポインタ位置の行番号の取得とコンテキストメニューの表示
Dim htInfo As FarPoint.Win.Spread.HitTestInformation = FpSpread1.HitTest(e.X, e.Y)
If htInfo.Type = FarPoint.Win.Spread.HitTestType.RowHeader Then
' 行ヘッダの場合
currentRow = htInfo.HeaderInfo.Row
customMenu.Show(FpSpread1, e.X, e.Y)
ElseIf htInfo.Type = FarPoint.Win.Spread.HitTestType.Viewport Then
' 通常セルの場合
currentRow = htInfo.ViewportInfo.Row
customMenu.Show(FpSpread1, e.X, e.Y)
End If
End If
End Sub
End Class
Private customMenu As ContextMenuStrip
Private currentRow As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' コンテキストメニューの作成
customMenu = New ContextMenuStrip()
customMenu.Items.Add("行の挿入")
customMenu.Items.Add("行の削除")
AddHandler customMenu.ItemClicked, AddressOf menu_ItemClicked
' SPREADの設定
For i As Integer = 0 To FpSpread1.ActiveSheet.RowCount - 1
FpSpread1.ActiveSheet.SetValue(i, 0, i)
Next
End Sub
Private Sub menu_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
' コンテキストメニューのクローズ
DirectCast(sender, ContextMenuStrip).Close()
' 処理の実行
Select Case e.ClickedItem.Text
Case "行の削除"
FpSpread1.ActiveSheet.RemoveRows(currentRow, 1)
Case "行の挿入"
FpSpread1.ActiveSheet.AddRows(currentRow, 1)
End Select
End Sub
Private Sub FpSpread1_MouseDown(sender As Object, e As MouseEventArgs) Handles FpSpread1.MouseDown
' マウスの右ボタンが押下された場合
If e.Button = MouseButtons.Right Then
' マウスポインタ位置の行番号の取得とコンテキストメニューの表示
Dim htInfo As FarPoint.Win.Spread.HitTestInformation = FpSpread1.HitTest(e.X, e.Y)
If htInfo.Type = FarPoint.Win.Spread.HitTestType.RowHeader Then
' 行ヘッダの場合
currentRow = htInfo.HeaderInfo.Row
customMenu.Show(FpSpread1, e.X, e.Y)
ElseIf htInfo.Type = FarPoint.Win.Spread.HitTestType.Viewport Then
' 通常セルの場合
currentRow = htInfo.ViewportInfo.Row
customMenu.Show(FpSpread1, e.X, e.Y)
End If
End If
End Sub
End Class
◎サンプルコード(C#)
private ContextMenuStrip customMenu;
private int currentRow;
private void Form1_Load(object sender, EventArgs e)
{
// コンテキストメニューの作成
customMenu = new ContextMenuStrip();
customMenu.Items.Add("行の挿入");
customMenu.Items.Add("行の削除");
customMenu.ItemClicked += menu_ItemClicked;
// SPREADの設定
for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
{
fpSpread1.ActiveSheet.SetValue(i, 0, i);
}
}
private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// コンテキストメニューのクローズ
ContextMenuStrip cm = (ContextMenuStrip)sender;
cm.Close();
// 処理の実行
switch (e.ClickedItem.Text)
{
case "行の削除":
fpSpread1.ActiveSheet.RemoveRows(currentRow, 1);
break;
case "行の挿入":
fpSpread1.ActiveSheet.AddRows(currentRow, 1);
break;
}
}
private void fpSpread1_MouseDown(object sender, MouseEventArgs e)
{
// マウスの右ボタンが押下された場合
if (e.Button==MouseButtons.Right)
{
// マウスポインタ位置の行番号の取得とコンテキストメニューの表示
FarPoint.Win.Spread.HitTestInformation htInfo = fpSpread1.HitTest(e.X, e.Y);
if(htInfo.Type == FarPoint.Win.Spread.HitTestType.RowHeader)
{
// 行ヘッダの場合
currentRow = htInfo.HeaderInfo.Row;
customMenu.Show(fpSpread1, e.X, e.Y);
}
else if (htInfo.Type == FarPoint.Win.Spread.HitTestType.Viewport)
{
// 通常セルの場合
currentRow = htInfo.ViewportInfo.Row;
customMenu.Show(fpSpread1, e.X, e.Y);
}
}
}
private int currentRow;
private void Form1_Load(object sender, EventArgs e)
{
// コンテキストメニューの作成
customMenu = new ContextMenuStrip();
customMenu.Items.Add("行の挿入");
customMenu.Items.Add("行の削除");
customMenu.ItemClicked += menu_ItemClicked;
// SPREADの設定
for (int i = 0; i < fpSpread1.ActiveSheet.RowCount; i++)
{
fpSpread1.ActiveSheet.SetValue(i, 0, i);
}
}
private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// コンテキストメニューのクローズ
ContextMenuStrip cm = (ContextMenuStrip)sender;
cm.Close();
// 処理の実行
switch (e.ClickedItem.Text)
{
case "行の削除":
fpSpread1.ActiveSheet.RemoveRows(currentRow, 1);
break;
case "行の挿入":
fpSpread1.ActiveSheet.AddRows(currentRow, 1);
break;
}
}
private void fpSpread1_MouseDown(object sender, MouseEventArgs e)
{
// マウスの右ボタンが押下された場合
if (e.Button==MouseButtons.Right)
{
// マウスポインタ位置の行番号の取得とコンテキストメニューの表示
FarPoint.Win.Spread.HitTestInformation htInfo = fpSpread1.HitTest(e.X, e.Y);
if(htInfo.Type == FarPoint.Win.Spread.HitTestType.RowHeader)
{
// 行ヘッダの場合
currentRow = htInfo.HeaderInfo.Row;
customMenu.Show(fpSpread1, e.X, e.Y);
}
else if (htInfo.Type == FarPoint.Win.Spread.HitTestType.Viewport)
{
// 通常セルの場合
currentRow = htInfo.ViewportInfo.Row;
customMenu.Show(fpSpread1, e.X, e.Y);
}
}
}
旧文書番号
84301