假设您使用的是 VCL,InplaceEditor 是 TCustomGrid 的属性。它是TInplaceEdit 类型,从TCustomEdit 下降。您可以在其中移动光标,就像TEdit。
如果您使用自动编辑单元格内容的方式,您可以使用以下方式移动光标。我已经对其进行了测试,它对我有用。 (我在 Windows 10 中使用柏林)
unit Main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids;
const
WM_MY_MESSAGE = WM_USER + 1;
type
TStringGridEx = class helper for TStringGrid
public
function GetInplaceEditor(): TInplaceEdit;
end;
TForm1 = class(TForm)
aGrid: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure aGridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
private
procedure OnMyMessage(var Msg: TMessage); message WM_MY_MESSAGE;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.aGridGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string);
begin
PostMessage(Handle, WM_MY_MESSAGE, 0, 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
y: Integer;
x: Integer;
begin
for y := 0 to aGrid.RowCount do
begin
for x := 0 to aGrid.ColCount do // fill the grid
aGrid.Cells[x, y] := Format('Col %d, Row %d'#13#10, [x, y]);
end;
end;
procedure TForm1.OnMyMessage(var Msg: TMessage);
var
pInplaceEdit: TInplaceEdit;
begin
pInplaceEdit := aGrid.GetInplaceEditor();
if Assigned(pInplaceEdit) then
begin
pInplaceEdit.SelStart := pInplaceEdit.EditText.TrimRight.Length;
pInplaceEdit.SelLength := 0;
end;
end;
{ TStringGridEx }
function TStringGridEx.GetInplaceEditor: TInplaceEdit;
begin
Result := InplaceEditor; // get access to InplaceEditor
end;
end.
山姆