[SOLVED] TStringGrid and editor lazarus / 31-LCL ---------------------------------------------------------------------- spassigl · 2021-01-31 15:45 Hi I have a problem with a custom TStringGrid. The Grid is created at runtime and the constructor is as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i constructor TMyGrid.Create(TheOwner: TComponent);begin   inherited Create(TheOwner);    FixedCols := 0;   FixedRows := 0;    GridLineWidth := 1;    Options := [goFixedVertLine, goFixedHorzLine, goVertLine, goEditing];   Options := Options + [goRowHighlight, goRowSelect];    ScrollBars := ssAutoBoth;    RowCount := 0;   ColCount := 2;    with Columns.Add do   begin     ReadOnly := true;   Alignment := taRightJustify;     Color := clBtnFace;   end;   with Columns.Add do   begin     ReadOnly := false;     Alignment := taLeftJustify;   end;    DefaultRowHeight := Font.GetTextHeight('Tg');end; So far so good. The problem is that when I select a cell for editing by pressing the Enter key, the editor shows up correctly, but the text is fully selected and cannot use the lef and right keys to move within the editor. Infact, if I use those keys the grid selects another cell (left or right of that one). If I instead click on the selected editor text all goes well and I can move around the text in the editor. Also: if I press enter on a cell to edit it, the OnEditingDone event is fired twice: at the beginning of the edit and at the end. If, instead, I click the cell to edit it, OnEditingDone is (correctly) fired once, i.e. at the end of the edit. Is there anything I'm missing here? Thanks ---------------------------------------------------------------------- wp · 2021-01-31 16:24 This is a limitation of the LCL TStringGrid. In fact, it is an incomatibility with Delphi and should be fixed. Somebody needs to write a bug report. You can work around by entering edit mode only by pressing F2 twice. The first F2 shows the editor, but the entire text is selected, the second F2 keeps the editor but removes the selection. Or you can enter edit mode by two clicks. You can save one F2 press or one mouse click when you activate AlwaysShowEditor in the grid's options. ---------------------------------------------------------------------- jamie · 2021-01-31 17:04 I am sure there is some flag lurking in there somewhere... but here is a work around.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin if Sender is TstringGrid then with TstringGrid(Sender) do Begin   if Editor is TStringCellEditor Then    Begin      if (TEdit(Editor).Sellength <> 0)and(Key= VK_Left) Then      TEDit(Editor).Sellength := 0;    end; end;end;                                                 Include the lclType unit for the key defines ---------------------------------------------------------------------- spassigl · 2021-01-31 17:14 I had tried that already but I'd get an Invalid Type Cast exception. Quote from: jamie on January 31, 2021, 06:04:25 pmI am sure there is some flag lurking in there somewhere... but here is a work around.... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin if Sender is TstringGrid then with TstringGrid(Sender) do Begin   if Editor is TStringCellEditor Then    Begin      if (TEdit(Editor).Sellength <> 0)and(Key= VK_Left) Then      TEDit(Editor).Sellength := 0;    end; end;end;                                                 Include the lclType unit for the key defines ---------------------------------------------------------------------- jamie · 2021-01-31 17:21 it compiles and runs fine here. what Laz version do you have ? ---------------------------------------------------------------------- wp · 2021-01-31 17:23 The line of inheritance is TStringCellEditor --> TCustomMaskEdit --> TCustomEdit TEdit --> TCustomEdit This means that TStringCellEditor is not a descendant of TEdit. Why don't you cast to TStingCellEditor? ---------------------------------------------------------------------- jamie · 2021-01-31 17:26 Well it works fine for me here because there exist at the base a TEDIT where these properties are. I did this with an older version of laz with 3.0.4 but I don't see where this has changed any in current state. ---------------------------------------------------------------------- jamie · 2021-01-31 17:30 Ok, a remapped version Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin if Sender is TstringGrid then with TstringGrid(Sender) do Begin   if Editor is TStringCellEditor Then with Editor As TStringCellEditor do    Begin     If  (Sellength <> 0)and(Key= VK_Left) Then        Sellength := 0;    end; end;end;                         Works either way for me.. ---------------------------------------------------------------------- spassigl · 2021-01-31 17:31 2.0.10 FPC 3.2.0 On Linux/GTK This is what I had done. Casting to TStringCellEditor doesn't work, either. I had also tried TCustomMaskEdit to no avail. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyGrid.MyGridKeyDown(Sender: TObject; var Key: Word;   Shift: TShiftState);begin   case Key of     VK_LEFT, VK_RIGHT:         if Editor is TStringCellEditor then          begin               if (TEdit(Editor).Sellength <> 0) then                 TEdit(Editor).SelLength := 0;          end;      VK_DELETE:         if ssCtrl in Shift then         begin            DeleteRow(Row);          Key := 0;         end;   end; // caseend; constructor TMyGrid.Create(TheOwner: TComponent);begin   inherited Create(TheOwner);    [Additional setup omitted]    OnKeyDown   := @MyGridKeyDown;end;  Quote from: jamie on January 31, 2021, 06:21:13 pmit compiles and runs fine here. what Laz version do you have ? Quote from: jamie on January 31, 2021, 06:21:13 pmit compiles and runs fine here. what Laz version do you have ? ---------------------------------------------------------------------- spassigl · 2021-01-31 17:33 Nope it doesn't work for me. Quote from: jamie on January 31, 2021, 06:30:09 pmOk, a remapped version Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin if Sender is TstringGrid then with TstringGrid(Sender) do Begin   if Editor is TStringCellEditor Then with Editor As TStringCellEditor do    Begin     If  (Sellength <> 0)and(Key= VK_Left) Then        Sellength := 0;    end; end;end;                         Works either way for me.. ---------------------------------------------------------------------- wp · 2021-01-31 17:34 Better to study the source code: In TStringCellEditor.KeyDown for VK_LEFT and VK_RIGHT, there is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       if GetFastEntry then begin        IntSel:=          ((Key=VK_LEFT) and not AtStart) or          ((Key=VK_RIGHT) and not AtEnd);      if not IntSel then begin          doGridKeyDown; This "GetFastEntry" looks interesting: A "Ctrl-Click" on it moves the source editor to this procedure: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   function GetFastEntry: boolean;  begin    if FGrid<>nil then      Result := FGrid.FastEditing    else      Result := False;  end; And here we have a FGrid.FastEditing. I suppose that "Fast" is understood here as a method to type text into a cell, press the right- arrow to move to the next cell and continue typing there. In Delphi however the cursor does not move out of the cell when Left/right is pressed. What if we set FastEditing to false -- it is a public property of TCustomDrawGrid, the ancestor of TStringGrid. Voila - we have the Delphi behaviour. So, all you have to do, is to set FastEditing to false in your grid constructor. P.S. It's even mentioned in the Grids wiki: https://wiki.lazarus.freepascal .org/Grids_Reference_Page#Ways_in_which_you_can_make_a_Lazarus_grid_mo re_Delphi-compatible ---------------------------------------------------------------------- spassigl · 2021-01-31 17:37 That fixed it! Thanks! ---------------------------------------------------------------------- jamie · 2021-01-31 18:17 @Wp  I consider the fastEditing an undesirable feature with absolutely no idea of what it's good for other then causing bugs.  why isn't this set to false by default ? and why isn't it a published property ? ---------------------------------------------------------------------- wp · 2021-01-31 18:43 Don't ask me...