MultiRow Windows Forms > Developer's Guide > Using MultiRow > Grid > Context Menu |
You can set the context menu (right-click menu) on the grid as well as on the cell editing control in the GcMultiRow control.
Assign the context menu to be displayed on the grid with the GcMultiRow.ContextMenu property or the GcMultiRow.ContextMenuStrip property. If a menu has been assigned to both properties, the GcMultiRow.ContextMenu property has priority.
This example sets the context menu.
Dim menu As ContextMenuStrip = New ContextMenuStrip() menu.Items.AddRange(New ToolStripItem() { _ New ToolStripMenuItem("Test 1"), New ToolStripMenuItem("Test 2") }) GcMultiRow1.ContextMenuStrip = menu |
ContextMenuStrip menu = new ContextMenuStrip(); menu.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem("Test 1"), new ToolStripMenuItem("Test 2") }); gcMultiRow1.ContextMenuStrip = menu; |
By default, the context menu can be displayed by right clicking on it with the mouse, or by using the application key. The application key can be changed using the Shift + F10 key.
Use the GcMultiRow.HitTest method to find the type of object being pointed to by the mouse. For example, you can have different actions when the mouse points to the header and when the mouse points to the cell.
The following code changes the background color of a cell in a row when the mouse button is clicked on that cell.
Imports GrapeCity.Win.MultiRow Private Sub GcMultiRow1_MouseDown(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles GcMultiRow1.MouseDown Dim gcMultiRow As GcMultiRow = TryCast(sender, GcMultiRow) Dim hitTestInfo As HitTestInfo = gcMultiRow.HitTest(e.X, e.Y) If hitTestInfo.Type = HitTestType.Row Then If hitTestInfo.CellIndex > -1 Then gcMultiRow(hitTestInfo.SectionIndex, hitTestInfo.CellIndex).Style.BackColor = Color.Azure End If End If End Sub |
using GrapeCity.Win.MultiRow; private void gcMultiRow1_MouseDown(object sender, MouseEventArgs e) { GcMultiRow gcMultiRow = sender as GcMultiRow; HitTestInfo hitTestInfo = gcMultiRow.HitTest(e.X, e.Y); if (hitTestInfo.Type == HitTestType.Row) { if (hitTestInfo.CellIndex > -1) { gcMultiRow[hitTestInfo.SectionIndex, hitTestInfo.CellIndex].Style.BackColor = Color.Azure; } } } |
The context menu can be set at the row level as well.
The following code assigns the context menu to the first row. This menu will be displayed on places other than the cells, or when the context menu has not been specified for the cells.
Dim menu As ContextMenuStrip = New ContextMenuStrip() menu.Items.AddRange(New ToolStripItem() { _ New ToolStripMenuItem("Test 1"), New ToolStripMenuItem("Test 2") }) GcMultiRow1.Rows(0).ContextMenuStrip = menu |
ContextMenuStrip menu = new ContextMenuStrip(); menu.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem("Test 1"), new ToolStripMenuItem("Test 2") }); gcMultiRow1.Rows[0].ContextMenuStrip = menu; |
The context menu can be specified for each cell by using the Cell.ContextMenuStrip property.
The following code describes how to set the context menu for the first cell of the first row.
Dim menu As ContextMenuStrip = New ContextMenuStrip() menu.Items.AddRange(New ToolStripItem() { _ New ToolStripMenuItem("Test 1"), New ToolStripMenuItem("Test 2") }) GcMultiRow1.Rows(0).Cells(0).ContextMenuStrip = menu |
ContextMenuStrip menu = new ContextMenuStrip(); menu.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem("Test 1"), new ToolStripMenuItem("Test 2") }); gcMultiRow1.Rows[0].Cells[0].ContextMenuStrip = menu; |
The context menu can be displayed while editing the cell if the cell editing control supports the context menu.
This example illustrates the cell displaying the context menu while being edited.
Imports GrapeCity.Win.MultiRow Private Sub GcMultiRow1_EditingControlShowing(ByVal sender As System.Object, ByVal e As GrapeCity.Win.MultiRow.EditingControlShowingEventArgs) Handles GcMultiRow1.EditingControlShowing Dim textBoxEditingControl As TextBoxEditingControl = DirectCast(e.Control, TextBoxEditingControl) If textBoxEditingControl IsNot Nothing Then Dim menu As ContextMenuStrip = New ContextMenuStrip() menu.Items.AddRange(New ToolStripItem() { New ToolStripMenuItem("Test 1"), New ToolStripMenuItem("Test 2") }) textBoxEditingControl.ContextMenuStrip = menu End If End Sub |
using GrapeCity.Win.MultiRow; private void gcMultiRow1_EditingControlShowing( object sender, EditingControlShowingEventArgs e) { TextBoxEditingControl textBoxEditingControl = e.Control as TextBoxEditingControl; if (textBoxEditingControl != null) { ContextMenuStrip menu = new ContextMenuStrip(); menu.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem("Test 1"), new ToolStripMenuItem("Test 2") }); textBoxEditingControl.ContextMenuStrip = menu; } } |