Hi I am building an invoice project using a TStringGrid to enter data (I am using a grid to keep the individual items and their amounts aligned). Because of the size of the final report I am limiting each row length to 47 characters and after that the next row will receive the onKeyUp event. This is easy to do
procedure TForm1.ngridKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
s, s2:string;
p, l:Integer;
begin
if Length(ngrid.Cells[ngrid.col,ngrid.row]) =47 then
ngrid.Row:=ngrid.Row+1;
end;
But I don't want to split a word so I find the last space, trim the cell text to that point and put the remainder on the next line
procedure TForm1.ngridKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
s, s2:string;
p, l:Integer;
begin
if Length(ngrid.Cells[ngrid.col,ngrid.row]) =47 then
begin
s:= ngrid.Cells[ngrid.col,ngrid.row];
l:= Length(s);
p:=LastDelimiter(' ',s);
s2:=RightStr(s,l-p);
ngrid.Cells[ngrid.col,ngrid.row]:=LeftStr(s,p);
ngrid.Row:=ngrid.Row+1;
ngrid.cells[ngrid.col,ngrid.row]:=s2;
end;
end;
This works
but the text in the new cell must be selected because the next character typed clears the cell. Like so
How do I stop the cell text being selected or move the cursor to the end?