gpt4 book ai didi

delphi - 如何在Delphi StringGrid单元格中移动光标位置?

转载 作者:行者123 更新时间:2023-12-02 00:12:40 24 4
gpt4 key购买 nike

当您有一个设置了 goEditing 选项的 TStringGrid 并且单元格中有多行文本时,当您通过单击该单元格来编辑该单元格时,光标将位于该文本的最末尾。如何将光标移动到另一个位置?我的特殊问题是,如果文本末尾有回车符,用户会认为单元格为空。我想在任何回车符之前移动光标。

最佳答案

假设您使用的是VCLInplaceEditorTCustomGrid 的一个属性。它的类型为 TInplaceEdit,源自 TCustomEdit。您可以将光标移动到其中,就像 TEdit 一样。

如果您使用的是自动编辑单元格内容的方式,则可以使用以下方式来移动光标。我已经测试过它并且它对我有用。 (我在 Windows 10 中使用 Berlin)

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.

山姆

关于delphi - 如何在Delphi StringGrid单元格中移动光标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com