Example: Invoke an edit dialog on double-clicking
- 2 minutes to read
The following example shows how you can open your EditRecord dialog when the end-user double-clicks within a grid View.
The View’s OnDblClick event is fired on double-clicking. The GetCursorPos function provides the current coordinates of the event. However, these coordinates are calculated relative to the top-left corner of the screen and you need to convert them to the View’s client coordinates. For this purpose, you should use the ScreenToClient method of the grid site containing the View.
After the coordinates are obtained, the clicked record is determined by using HitTest functionality. The GetHitTest function returns a HitTest object that identifies the grid element located at a particular position. If a user has clicked a record or its cell, GetHitTest will return a TcxGridRecordHitTest object or its descendant and the GridRecord property will identify the record itself.
procedure TForm1.tvCustomersDblClick(Sender: TObject);
var
APoint: TPoint;
AHitTest: TcxCustomGridHitTest;
ARecord: TcxCustomGridRecord;
begin
GetCursorPos(APoint);
with Grid.FocusedView.Site do
begin
APoint := ScreenToClient(APoint);
AHitTest := ViewInfo.GetHitTest(APoint);
if AHitTest is TcxGridRecordHitTest then
begin
ARecord := TcxGridRecordHitTest(AHitTest).GridRecord;
//Call your procedure to activate an edit dialog for the clicked record
OpenEditRecordDialog(ARecord);
end;
end;
end;