Back to Devexpress

Hit Information

windowsforms-1807-controls-and-libraries-pivot-grid-miscellaneous-hit-information.md

latest5.1 KB
Original Source

Hit Information

  • May 25, 2020
  • 3 minutes to read

Sometimes you may need to define which element is located at particular screen coordinates. For instance, you may have to define which element of a Pivot Grid Control an end-user has clicked. For this purpose, PivotGridControl implements the PivotGridControl.CalcHitInfo method. It accepts a specific point, measured in Pivot Grid Control client coordinates as its pt parameter, and returns a hit info object that contains information on the corresponding element.

The PivotGridHitInfo object exposes information about a test point via a number of its properties that can be grouped into two categories:

Tip

Demo:

Requires installation of WinForms Subscription. Download.

Example: How to Copy Data to the Clipboard

Assume that the PopupMenu component has already been placed on a form. It has a single menu item - “Copy to Clipboard”. This context menu is invoked when an end-user right-clicks within the Data Area. The item’s ItemClick event is handled to copy data to the Clipboard. To copy the selected cells to the Clipboard the PivotGridCells.CopySelectionToClipboard method is used.

To determine whether an end-user has right-clicked within the Data Area, the PivotGridControl.CalcHitInfo method is used. It returns the PivotGridHitInfo object whose PivotGridHitInfo.HitTest property identifies whether the cell has been clicked.

The image below shows the result.

csharp
using DevExpress.XtraPivotGrid;
// ...
private void Form1_Load(object sender, EventArgs e) {
    BarButtonItem btnCopyToClipboard = new BarButtonItem(barManager1, "Copy to Clipboard");
    btnCopyToClipboard.Tag = pivotGridControl1;
    popupMenu1.AddItem(btnCopyToClipboard);
    btnCopyToClipboard.ItemClick += BtnCopyToClipboard_ItemClick;
}

private void BtnCopyToClipboard_ItemClick(object sender, ItemClickEventArgs e) {
    PivotGridControl pivot = (e.Item.Tag as PivotGridControl);
    pivot.Cells.CopySelectionToClipboard();
}

private void pivotGridControl1_MouseUp(object sender, MouseEventArgs e) {
    PivotGridControl pivot = sender as PivotGridControl;
    Point pt = new Point(e.X, e.Y);
    if (e.Button != MouseButtons.Right) return;
    if (pivot.CalcHitInfo(pt).HitTest != PivotGridHitTest.Cell) return;
    // Shows the context menu.
    popupMenu1.ShowPopup(pivot.PointToScreen(pt));
}
vb
Imports DevExpress.XtraPivotGrid
' ...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim btnCopyToClipboard As New BarButtonItem(barManager1, "Copy to Clipboard")
    btnCopyToClipboard.Tag = pivotGridControl1
    popupMenu1.AddItem(btnCopyToClipboard)
    AddHandler btnCopyToClipboard.ItemClick, AddressOf BtnCopyToClipboard_ItemClick
End Sub

Private Sub BtnCopyToClipboard_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    Dim pivot As PivotGridControl = (TryCast(e.Item.Tag, PivotGridControl))
    pivot.Cells.CopySelectionToClipboard()
End Sub

Private Sub pivotGridControl1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim pivot As PivotGridControl = TryCast(sender, PivotGridControl)
    Dim pt As New Point(e.X, e.Y)
    If e.Button <> MouseButtons.Right Then
        Return
    End If
    If pivot.CalcHitInfo(pt).HitTest <> PivotGridHitTest.Cell Then
        Return
    End If
    ' Shows the context menu.
    popupMenu1.ShowPopup(pivot.PointToScreen(pt))
End Sub