Sometimes in applications you may need to identify which element is located at specific coordinates. For instance, you may need to determine which part of a VGridControl control has been clicked or determine if dropping is allowed to a particular control's area when performing drag and drop operations.
For the purposes described above, use the grid control's VGridControlBase.CalcHitInfo method. It accepts a point in grid client coordinates as a parameter and returns a newly created VGridHitInfo object containing information on the grid's corresponding element. Note that you can also pass a point located outside the grid control. The coordinates should also be measured from the grid control's top left corner.
The returned VGridHitInfo object exposes information about the point passed via a number of its properties. Use the VGridHitInfo.BandIndex, VGridHitInfo.CellIndex and VGridHitInfo.RecordIndex properties to get the index of a Band, cell or record which the specified point belongs to. If a Row is at the point specified, you can determine which one it is by the VGridHitInfo.Row property. The VGridHitInfo.HitInfoType property can be used to get the type of an element residing under the test point. This property returns one of the VGridHitInfo enumeration values.
The following example demonstrates how to process information provided by the VGridHitInfo object. It handles the grid's System.Windows.Forms.Control.MouseMove event and displays information about a grid element under the mouse cursor. This example assumes that the form contains the VGridControl and System.Windows.Forms.ListBox controls.
C# |
private void vGridControl1_MouseMove(object sender, MouseEventArgs e) {
VGridControl vGrid = sender as VGridControl;
Point pt = new Point(e.X, e.Y);
VGridHitInfo hitInfo = vGrid.CalcHitInfo(pt);
listBox1.Items.Clear();
listBox1.Items.Add("BandIndex: " + hitInfo.BandIndex);
listBox1.Items.Add("CellIndex: " + hitInfo.CellIndex);
listBox1.Items.Add("PtMouse: " + hitInfo.PtMouse);
listBox1.Items.Add("RecordIndex: " + hitInfo.RecordIndex);
listBox1.Items.Add("HitInfoType: " + hitInfo.HitInfoType);
if (hitInfo.Row == null) return;
string rowCaption = "";
if (hitInfo.CellIndex != -1)
rowCaption = hitInfo.Row.GetRowProperties(hitInfo.CellIndex).Caption;
else
for (int i=0; i < hitInfo.Row.RowPropertiesCount; i++)
rowCaption += hitInfo.Row.GetRowProperties(i).Caption + " ";
listBox1.Items.Add("RowName: " + hitInfo.Row.Name);
listBox1.Items.Add("RowCaption: " + rowCaption);
}
|
VB |
Private Sub VGridControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles VGridControl1.MouseMove
Dim vGrid As VGridControl = sender
Dim pt As Point = New Point(e.X, e.Y)
Dim hitInfo As DevExpress.XtraVerticalGrid.VGridHitInfo
Dim rowCaption As String = ""
Dim i As Integer
hitInfo = vGrid.CalcHitInfo(pt)
ListBox1.Items.Clear()
ListBox1.Items.Add("BandIndex: " + hitInfo.BandIndex.ToString())
ListBox1.Items.Add("CellIndex: " + hitInfo.CellIndex.ToString())
ListBox1.Items.Add("PtMouse: " + hitInfo.PtMouse.ToString())
ListBox1.Items.Add("RecordIndex: " + hitInfo.RecordIndex.ToString())
ListBox1.Items.Add("HitInfoType: " + hitInfo.HitInfoType.ToString())
If hitInfo.Row Is Nothing Then
Return
End If
If hitInfo.CellIndex <> -1 Then
rowCaption = hitInfo.Row.GetRowProperties(hitInfo.CellIndex).Caption
Else
For i = 0 To hitInfo.Row.RowPropertiesCount - 1
rowCaption = rowCaption & hitInfo.Row.GetRowProperties(i).Caption + " "
Next
End If
ListBox1.Items.Add("RowName: " + hitInfo.Row.Name)
ListBox1.Items.Add("RowCaption: " + rowCaption)
End Sub
|
The image below illustrates the sample application. Note that the multiple records view layout is applied to the grid control in this example (the grid's VGridControl.LayoutStyle property is set to LayoutViewStyle.MultiRecordView). In this layout the control is considered to have a single band. So the VGridHitInfo.BandIndex property always returns 0.
