Apps key in DevExpress GridControl

Introduction

In Windows, context menus are displayed either by right-clicking on the mouse, or by pressing the Apps key on the keyboard. This post describes how to implement the same functionality on a DevExpress GridControl.

Background

The Apps key is usually to the left of the right Ctrl button on the keyboard.

Using the Code

Compile the project and run it. Set in the Input folder field a folder which contains some files. When pressing the Get files button, the grid is populated with the list of files of that folder. When pressing the Apps key on a selected row, the context menu appears. The context menu is displayed by the code:

Public Delegate Sub ContextMenuShow(ByVal v As GridView, ByVal pt As System.Drawing.Point)

Protected Overridable Sub ContextGridMenuShow_
	(ByVal v As GridView, ByVal pt As System.Drawing.Point)
    ContextMenuStrip1.Show(v.GridControl, pt)
End Sub

Private Shared Sub View_KeyDown( _
    ByVal v As GridView, ByVal e As KeyEventArgs, ByVal f As ContextMenuShow)
    If e.KeyCode = Keys.Apps Then
        If v.FocusedRowHandle > -1 Then
            Dim col As DevExpress.XtraGrid.Columns.GridColumn = v.FocusedColumn
            Dim gvi As ViewInfo.GridViewInfo = col.View.GetViewInfo()
            Dim rvi As ViewInfo.GridDataRowInfo = _
            gvi.RowsInfo(v.FocusedRowHandle - gvi.RowsLoadInfo.TopVisibleRowIndex)
            Dim i As Integer = _
            IIf(col.VisibleIndex < rvi.Cells.Count - 1, col.VisibleIndex + 1, 1)
            Dim rect As System.Drawing.Rectangle = rvi.Cells(i).Bounds
            If Not rect.IsEmpty Then
                Dim pt As System.Drawing.Point = rect.Location : pt.Offset(20, 10)
                f(v, pt)
            End If
        End If
    End If
End Sub

Protected Overridable Sub GridView1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridView1.KeyDown
    If e.KeyCode = Keys.Enter Then
        ShowEditForm()
    End If
    View_KeyDown(sender, e, AddressOf ContextGridMenuShow)
End Sub

The View_KeyDown method checks if the Apps key was pressed. If it was, and a row on the grid is focused, it gets the focused column. The visible row is retrieved through v.FocusedRowHandle - gvi.RowsLoadInfo.TopVisibleRowIndex, because the grid may have been scrolled. The cell next to the focused column is taken, or the second cell is taken if the focused column is larger than the displayed columns. For this cell, the bounds are taken and a point with an offset of 20 and 10 from the upper left corner is taken. For that point, a function is called that displays the context menu at that point (ContextGridMenuShow).

Points of Interest

This project also shows how a detail form may be displayed when double-clicking or pressing Enter on a row.

History

  • 28th June, 2011: Initial post
推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"