@@ -8,7 +8,7 @@ interface
88 Winapi.Windows, Vcl.Forms, Vcl.Graphics, Winapi.Messages, VirtualTrees, VirtualTrees.BaseTree, VirtualTrees.Types, Vcl.ComCtrls, System.SysUtils, System.Classes,
99 Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.CheckLst, Vcl.Controls, System.Types, Vcl.Dialogs, Vcl.Menus, Vcl.Mask, System.DateUtils, System.Math,
1010 dbconnection, dbstructures, apphelpers, texteditor, bineditor, gnugettext,
11- System.StrUtils, System.UITypes, SynRegExpr, Vcl.Themes, extra_controls;
11+ System.StrUtils, System.UITypes, SynRegExpr, Vcl.Themes, extra_controls, System.TypInfo ;
1212
1313type
1414 // Radio buttons and checkboxes which do not pass <Enter> key to their parent control
@@ -298,10 +298,16 @@ destructor TBaseGridEditorLink.Destroy;
298298 end ;
299299 if not Assigned(NewNode) then
300300 Break;
301- if not FTree.CanEdit(NewNode, NewColumn) then
301+ // Skip columns which cannot be focused or edited, e.g. the row number
302+ // column in data grids, which no editor can be created for.
303+ if not FTree.Header.AllowFocus(NewColumn) or not FTree.CanEdit(NewNode, NewColumn) then
302304 Continue;
303305 FTree.ClearSelection;
304306 FTree.Selected[NewNode] := True;
307+ // EditNode only sets the focused node, not the focused column, so without
308+ // this the cell focus stays on the previous column, visibly wrong when
309+ // Tab jumps from the last column back to the first column of a row.
310+ FTree.FocusedColumn := NewColumn;
305311 FTree.EditNode(NewNode, NewColumn);
306312 Break;
307313 end ;
@@ -371,6 +377,8 @@ function TBaseGridEditorLink.EndEditHelper(NewText: String): Boolean;
371377end ;
372378
373379procedure TBaseGridEditorLink.ReleaseEditorControl (AControl: TWinControl);
380+ var
381+ NullMethod: TMethod;
374382begin
375383 // Do not free editor controls synchronously here. Our destructor regularly
376384 // runs while such a control is still processing a message, as its OnKeyDown
@@ -381,6 +389,13 @@ procedure TBaseGridEditorLink.ReleaseEditorControl(AControl: TWinControl);
381389 // VCL's key dispatch.
382390 if (csDestroying in AControl.ComponentState) or (csCreating in AControl.ControlState) then
383391 Exit;
392+ // Prevent the deferred control from calling back into this destroyed editor
393+ // link. Most importantly, the OnExit event fires on the focus change when
394+ // another editor link takes over the focus, e.g. when Tab moves the edit to
395+ // the next cell. Without this, the stale handler would end the *new* edit
396+ // immediately after it was started, and also run on freed memory.
397+ NullMethod := Default(TMethod);
398+ SetMethodProp(AControl, ' OnExit' , NullMethod);
384399 if FParentForm is TExtForm then
385400 TExtForm(FParentForm).DeferFreeControl(AControl)
386401 else
0 commit comments