Recent

Author Topic: Edit behaviour in TStringGrid  (Read 13015 times)

marclebrun

  • New Member
  • *
  • Posts: 12
Edit behaviour in TStringGrid
« on: July 20, 2009, 09:29:56 am »
Hi all  ;)

In Delphi, when a TStringGrid is configured for editing, pressing Enter edits the current cell. The whole cell content is selected and pressing the right arrow key moves the cursor to the end of the text, allowing to add some characters at the end.

In Lazarus, things are slightly different : pressing Enter edits the current cell and the whole content is selected, BUT pressing the right arrow key moves to the next cell !!! and thus cancels the editing...

I didn't find a way to change that.
What I'd like to do is either move to the end of the selection (like in Delphi) or disable the selection of the whole cell content when pressing Enter.

Actually, if you press F2 the text is not selected and the cursor is at the beginning of the text. This could be ok but users do not usually press F2. I'm converting an old Delphi app and my users are used to pressing Enter to edit, so I wouldn't like to disturb them...  ::)

I'm using 0.9.26.2 on Windows XP.

If someone has an idea...
Many Thanks !!!   :D

Marc.

marclebrun

  • New Member
  • *
  • Posts: 12
Re: Edit behaviour in TStringGrid
« Reply #1 on: July 20, 2009, 09:51:10 am »
I have tried a temporary solution and it works!
On the KeyDown event of the grid, if the key is VK_RETURN and we are not in EditorMode, I change the key to VK_F2.

Here's the code:

Code: [Select]
procedure TForm1.GridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Shift=[]) and (Key=VK_RETURN) and (not Grid.EditorMode) then Key := VK_F2;
end;

(at least this will work on Windows...)


wimpie

  • New Member
  • *
  • Posts: 32
Re: Edit behaviour in TStringGrid
« Reply #3 on: January 11, 2010, 08:50:28 pm »
The code posted by marclebrun did not work for me.

On WinXP, Lazarus r23345 I had to use the following, slightly longer code

Code: [Select]
procedure TFormMain.GridKeyDown(Sender: TObject; var Key: Word;
    Shift: TShiftState);
var
    Editor: TStringCellEditor;
begin
    Editor := TStringCellEditor(StringGridIpAddress.Editor);

    if (Grid.EditorMode) and (Shift=[]) and (Editor.SelLength>0) and
       (Editor.SelLength=Length(Editor.Text)) and
       (Key in [VK_RIGHT, VK_LEFT]) then
    begin
        Editor.SelLength := 0;
        Editor.SelStart := Length(Editor.Text);
        Key := 0;
    end;
end;