TAdvStringGrid caret movement...

Hello,

Delphi 10.2 Version 25.0.31059.3231
Version 8.7.1.5

When the user selects the up/down arrow the cursor insertion point moves up/down a row and is placed at the end of the text in the destination cell.

I need the insertion point in the destination cell to be the same position as in the origination cell.

Suggestion how to accomplish?

Thanks,

Mark

Sorry, at this moment, there is unfortunately not a built-in feature with which this behavior can be selected.

Thanks for the response.

I am attempting to get the value of mnGrid.InplaceRichEdit.SelStart in:
mnGridCellValidate
mnGridEditCellDone
mnGridKeyDown
mnGridKeyUp

with no joy.
Take mnGridCellValidate for example, right before the callback is issued the value of SelStart is correct (in the TAdvInplaceEdit source). But, when I use mnGrid.InplaceRichEdit.SelStart in the callback it is always 0. Same for the others I have tested.

What am I doing wrong?

So, you are using the editor type edRichEdit? Or what exact type do you use?

Thanks for the response.

Excellent question.

I am using the default text editor. Looking around in code for the current insertion point, I found

if Assigned(mnGrid.CellEditor) then <- this was true

then

if Assigned(mnGrid.InplaceRichEdit) then <- this was true.

So I started trying to get the caret position from it.

Funny, I just added:

 if (ACol = 1) then
  AEditor:=edRichEdit;

And the arrow keys to move from row to row stopped working.

Back to using the default editor, which property has the caret position when keydown callback is made and which to set when keyup callback is called?

grid.NormalEdit.SelStart

Thanks Bruno.

Here is the test code that works, if anyone wants it.

unit Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, AdvUtil, Vcl.StdCtrls, Vcl.Grids,
  AdvObj, BaseGrid, AdvGrid;

type
  TForm2 = class(TForm)
    mnGrid: TAdvStringGrid;
    Memo1: TMemo;
    procedure FormShow(Sender: TObject);
    procedure mnGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure mnGridKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    keyDown:word;
    selStart:integer;
    procedure MemoLog(const s1:string);
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ TForm2 }

procedure TForm2.FormShow(Sender: TObject);
begin
 mnGrid.AllCells[1,1]:='Test line 1';
 mnGrid.AllCells[1,2]:='Another test line';
 mnGrid.AllCells[1,4]:='Last test line';
end;

procedure TForm2.MemoLog(const s1: string);
begin
 Memo1.Lines.Add(s1);
end;

procedure TForm2.mnGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
 keyS:string;
begin
 selStart:=-1;
 if (mnGrid.SelectedCellsCount <> 1) then
  Exit;

 if (Key = VK_UP) then
  keyS:='VK_UP'
 else if (Key = VK_DOWN) then
  keyS:='VK_DOWN'
 else
  Exit;

 keyDown:=key;
 selStart:=mnGrid.NormalEdit.SelStart;

 MemoLog('mnGridKeyDown:' + keyS  + ' col:' + mnGrid.col.ToString + ' row:' + mnGrid.Row.ToString);
 MemoLog(mnGrid.NormalEdit.SelStart.ToString);
end;

procedure TForm2.mnGridKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
 keyS:string;
begin
 if (key <> keyDown) then
  Exit;

 if (selStart = -1) then
  Exit;

 mnGrid.NormalEdit.SelStart:=selStart;

 if (Key = VK_UP) then
  keyS:='VK_UP'
 else if (Key = VK_DOWN) then
  keyS:='VK_DOWN'
 else
  Exit;

 MemoLog('mnGridKeyUp:' + keyS  + ' col:' + mnGrid.col.ToString + ' row:' + mnGrid.Row.ToString);
 MemoLog(mnGrid.NormalEdit.SelStart.ToString);
end;

end.
1 Like