MESCIUS CalendarGrid for Windows Forms 4.0J
コンテキストメニューを表示する

GcCalendarGrid.ContextMenuStripプロパティに任意のContextMenuStripコントロールを割り当てることで実行時にCalendarGrid上でユーザーが右クリックした際にコンテキストメニューを表示できます。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim contextMenu As New ContextMenuStrip()
    Dim menuItem1 As New ToolStripMenuItem("項目1")
    Dim menuItem2 As New ToolStripMenuItem("項目2")
    contextMenu.Items.AddRange(New ToolStripItem() {menuItem1, menuItem2})

    GcCalendarGrid1.ContextMenuStrip = contextMenu
End Sub
private void Form1_Load(object sender, EventArgs e)
{
    var contextMenu = new ContextMenuStrip();
    var menuItem1 = new ToolStripMenuItem("項目1");
    var menuItem2 = new ToolStripMenuItem("項目2");
    contextMenu.Items.AddRange(new ToolStripItem[] { menuItem1, menuItem2 });

    gcCalendarGrid1.ContextMenuStrip = contextMenu;
}

CalendarGrid内の特定の要素を選択した場合にだけコンテキストメニューを表示するには、GcCalendarGrid.ContextMenuStripプロパティにはContextMenuStripコントロールを割り当てず、マウスイベントでContextMenuStripコントロールを表示します。
次のコードは、CalendarGridで今日の日付が右クリックされた場合にのみコンテキストメニューを表示します。

Imports GrapeCity.Win.CalendarGrid

Private _contextMenu As ContextMenuStrip

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    _contextMenu = New ContextMenuStrip()
    Dim menuItem1 = New ToolStripMenuItem("項目1")
    Dim menuItem2 = New ToolStripMenuItem("項目2")
    _contextMenu.Items.AddRange(New ToolStripItem() {menuItem1, menuItem2})

    GcCalendarGrid1.Content(DateTime.Today).CellStyle.BackColor = Color.Azure
    GcCalendarGrid1.ScrollIntoView(DateTime.Today)
End Sub

Private Sub GcCalendarGrid1_MouseDown(sender As Object, e As MouseEventArgs) Handles GcCalendarGrid1.MouseDown
    If e.Button = MouseButtons.Right Then
        Dim gcCalendarGrid As GcCalendarGrid = DirectCast(sender, GcCalendarGrid)
        Dim hitTestInfo = gcCalendarGrid.HitTest(e.Location)
        If hitTestInfo.CellPosition.Date = DateTime.Today Then
            _contextMenu.Show(gcCalendarGrid, e.Location)
        End If
    End If
End Sub
using GrapeCity.Win.CalendarGrid;

ContextMenuStrip _contextMenu;

private void Form1_Load(object sender, EventArgs e)
{
    _contextMenu = new ContextMenuStrip();
    var menuItem1 = new ToolStripMenuItem("項目1");
    var menuItem2 = new ToolStripMenuItem("項目2");
    _contextMenu.Items.AddRange(new ToolStripItem[] { menuItem1, menuItem2 });

    gcCalendarGrid1.Content[DateTime.Today].CellStyle.BackColor = Color.Azure;
    gcCalendarGrid1.ScrollIntoView(DateTime.Today);
    gcCalendarGrid1.MouseDown += gcCalendarGrid1_MouseDown;
}

private void gcCalendarGrid1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        GcCalendarGrid gcCalendarGrid = sender as GcCalendarGrid;
        CalendarHitTestInfo hitTestInfo = gcCalendarGrid.HitTest(e.Location);
        if (hitTestInfo.CellPosition.Date == DateTime.Today)
        {
            _contextMenu.Show(gcCalendarGrid, e.Location);
        }
    }
}
関連トピック

 

 


© MESCIUS inc. All rights reserved.