PowerTools MultiRow for Windows Forms 8.0J
ContextMenuStrip プロパティ (Section)
使用例 

Sectionに関連付けられたショートカットメニューを取得または設定します。
構文
Public Property ContextMenuStrip As ContextMenuStrip
public ContextMenuStrip ContextMenuStrip {get; set;}

プロパティ値

Sectionに関連付けられたSystem.Windows.Forms.ContextMenuStrip。既定値はnull 参照 (Visual Basicでは Nothing)です。
解説

Cellを右クリックしたとき、セルのCell.ContextMenuStripが設定されていない場合はセクションのContextMenuStripが表示され、セクションのContextMenuStripも設定されていない場合はGcMultiRowのContextMenuStripが表示されます。

また、大量のデータを操作しているときにパフォーマンスペナルティが生じる事態を避けるため、DataBindingモードまたはVirtualModeモードでCellContextMenuStripNeededイベントまたはSectionContextMenuStripNeededイベントを処理できます。

オーナーテンプレートをGcMultiRowに追加する前にこのプロパティを設定することは推奨されません。

使用例
次のサンプルコードは、RowHeaderCellでContextMenuStripがポップアップしたときに、クリックされた行を非表示にする方法を示します。また、ContextMenuStripを通じて切り取り、コピー、および貼り付けを行うことができます。
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization;

namespace GrapeCity.Win.MultiRow.SampleCode
{
    class ContextMenuStripDemo : Form
    {
        private GcMultiRow gcMultiRow1 = new GcMultiRow();
        private FlowLayoutPanel panel = new FlowLayoutPanel();
        private Label label = new Label();

        ContextMenuStrip sectionMenuStrip = new ContextMenuStrip();
        ContextMenuStrip cellMenu = new ContextMenuStrip();

        ToolStripButton hideRow = new ToolStripButton("Hide Current Row");
        ToolStripButton cut = new ToolStripButton("Cut");
        ToolStripButton copy = new ToolStripButton("Copy");
        ToolStripButton paste = new ToolStripButton("Paste");

        public ContextMenuStripDemo()
        {
            this.Text = "ContextMenuStrip Demo";
            this.Size = new Size(700, 600);

            // Add MultiRow to Form
            this.gcMultiRow1.Dock = DockStyle.Fill;
            this.Controls.Add(this.gcMultiRow1);

            label.Dock = DockStyle.Bottom;
            label.BackColor = SystemColors.Info;
            label.Text = "Right Click the RowHeader to hide the row, right click the Cell to cut, copy or paste.";
            this.Controls.Add(label);

            this.Load += new EventHandler(Form1_Load);

            hideRow.Width = 90;
            hideRow.Click += new EventHandler(hideRow_Click);
            cut.Click += new EventHandler(cut_Click);
            copy.Click += new EventHandler(copy_Click);
            paste.Click += new EventHandler(paste_Click);

            sectionMenuStrip.Items.AddRange(new ToolStripItem[] { hideRow });
            cellMenu.Items.AddRange(new ToolStripItem[] { cut, copy, paste });
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Template template1 = Template.CreateGridTemplate(10, 80, 21, 1000, AutoGenerateGridTemplateStyles.RowHeaderAutoNumber);
            
            template1.Row.ContextMenuStrip = sectionMenuStrip;

            foreach (Cell item in template1.Row.Cells)
            {
                if (!(item is RowHeaderCell))
                {
                    item.ContextMenuStrip = cellMenu;
                }
            }

            gcMultiRow1.Template = template1;
            gcMultiRow1.RowCount = 20;
            gcMultiRow1.CellMouseDown += new EventHandler<CellMouseEventArgs>(gcMultiRow1_CellMouseDown);
        }

        void gcMultiRow1_CellMouseDown(object sender, CellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.Scope == CellScope.Row && gcMultiRow1.Template.Row.Cells[e.CellIndex].Selectable)
                {
                    gcMultiRow1.CurrentCellPosition = new CellPosition(e.Scope, e.SectionIndex, e.CellIndex);
                }
                if (gcMultiRow1.GetValue(e.SectionIndex, e.CellIndex) != null)
                {
                    cut.Enabled = true;
                    copy.Enabled = true;
                }
                else
                {
                    cut.Enabled = false;
                    copy.Enabled = false;
                }

                if (e.Scope == CellScope.Row && e.SectionIndex != gcMultiRow1.RowCount - 1)
                {
                    sectionMenuStrip.Enabled = true;
                    sectionMenuStrip.Tag = new CellPosition(e.Scope, e.SectionIndex, e.CellIndex);
                }
                else
                {
                    sectionMenuStrip.Enabled = false;
                }

                if (Clipboard.ContainsText())
                {
                    paste.Enabled = true;
                }
                else
                {
                    paste.Enabled = false;
                }
            }
        }

        void paste_Click(object sender, EventArgs e)
        {
            if (EditingActions.Paste.CanExecute(gcMultiRow1))
            {
                EditingActions.Paste.Execute(gcMultiRow1);
            }
        }

        void copy_Click(object sender, EventArgs e)
        {
            if (EditingActions.Copy.CanExecute(gcMultiRow1))
            {
                EditingActions.Copy.Execute(gcMultiRow1);
            }
        }

        void cut_Click(object sender, EventArgs e)
        {
            if(EditingActions.Cut.CanExecute(gcMultiRow1))
            {
                EditingActions.Cut.Execute(gcMultiRow1);
            }
        }

        void hideRow_Click(object sender, EventArgs e)
        {
            CellPosition position = (CellPosition)sectionMenuStrip.Tag;
            if (!position.IsEmpty)
            {
                gcMultiRow1.Rows[position.SectionIndex].Visible = false;
            }
        }
        
        [STAThreadAttribute()]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new ContextMenuStripDemo());
        }
    }
}
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Globalization
Imports GrapeCity.Win.MultiRow

Class ContextMenuStripDemo
    Inherits Form
    Friend WithEvents gcMultiRow1 As New GcMultiRow()
    Private panel As New FlowLayoutPanel()
    Private label As New Label()

    Friend WithEvents sectionMenuStrip As New ContextMenuStrip()
    Friend WithEvents cellMenu As New ContextMenuStrip()

    Friend WithEvents hideRow As New ToolStripButton("Hide Current Row")
    Friend WithEvents cut As New ToolStripButton("Cut")
    Friend WithEvents copy As New ToolStripButton("Copy")
    Friend WithEvents paste As New ToolStripButton("Paste")

    Public Sub New()
        Me.Text = "ContextMenuStrip Demo"
        Me.Size = New Size(700, 600)

        ' Add MultiRow to Form
        Me.gcMultiRow1.Dock = DockStyle.Fill
        Me.Controls.Add(Me.gcMultiRow1)

        label.Dock = DockStyle.Bottom
        label.BackColor = SystemColors.Info
        label.Text = "Right Click the RowHeader to hide the row, right click the Cell to cut, copy or paste."
        Me.Controls.Add(label)

        hideRow.Width = 90

        sectionMenuStrip.Items.AddRange(New ToolStripItem() {hideRow})
        cellMenu.Items.AddRange(New ToolStripItem() {cut, copy, paste})
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim template1 As Template = Template.CreateGridTemplate(10, 80, 21, 1000, AutoGenerateGridTemplateStyles.RowHeaderAutoNumber)

        template1.Row.ContextMenuStrip = sectionMenuStrip

        For Each item As Cell In template1.Row.Cells
            If Not (TypeOf item Is RowHeaderCell) Then
                item.ContextMenuStrip = cellMenu
            End If
        Next

        gcMultiRow1.Template = template1
        gcMultiRow1.RowCount = 20
    End Sub

    Private Sub gcMultiRow1_CellMouseDown(ByVal sender As Object, ByVal e As CellMouseEventArgs) Handles gcMultiRow1.CellMouseDown
        If e.Button = MouseButtons.Right Then
            If e.Scope = CellScope.Row AndAlso gcMultiRow1.Template.Row.Cells(e.CellIndex).Selectable Then
                gcMultiRow1.CurrentCellPosition = New CellPosition(e.Scope, e.SectionIndex, e.CellIndex)
            End If
            If gcMultiRow1.GetValue(e.SectionIndex, e.CellIndex) <> Nothing Then
                cut.Enabled = True
                copy.Enabled = True
            Else
                cut.Enabled = False
                copy.Enabled = False
            End If

            If e.Scope = CellScope.Row AndAlso e.SectionIndex <> gcMultiRow1.RowCount - 1 Then
                sectionMenuStrip.Enabled = True
                sectionMenuStrip.Tag = New CellPosition(e.Scope, e.SectionIndex, e.CellIndex)
            Else
                sectionMenuStrip.Enabled = False
            End If

            If Clipboard.ContainsText() Then
                paste.Enabled = True
            Else
                paste.Enabled = False
            End If
        End If
    End Sub

    Private Sub paste_Click(ByVal sender As Object, ByVal e As EventArgs) Handles paste.Click
        If EditingActions.Paste.CanExecute(gcMultiRow1) Then
            EditingActions.Paste.Execute(gcMultiRow1)
        End If
    End Sub

    Private Sub copy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles copy.Click
        If EditingActions.Copy.CanExecute(gcMultiRow1) Then
            EditingActions.Copy.Execute(gcMultiRow1)
        End If
    End Sub

    Private Sub cut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cut.Click
        If EditingActions.Cut.CanExecute(gcMultiRow1) Then
            EditingActions.Cut.Execute(gcMultiRow1)
        End If
    End Sub

    Private Sub hideRow_Click(ByVal sender As Object, ByVal e As EventArgs) Handles hideRow.Click
        Dim position As CellPosition = DirectCast(sectionMenuStrip.Tag, CellPosition)
        If Not position.IsEmpty Then
            gcMultiRow1.Rows(position.SectionIndex).Visible = False
        End If
    End Sub

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New ContextMenuStripDemo())
    End Sub
End Class
参照

Section クラス
Section メンバ
ContextMenuStrip プロパティ

 

 


© 2008-2015 GrapeCity inc. All rights reserved.