作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当您有一个设置了 goEditing 选项的 TStringGrid 并且单元格中有多行文本时,当您通过单击该单元格来编辑该单元格时,光标将位于该文本的最末尾。如何将光标移动到另一个位置?我的特殊问题是,如果文本末尾有回车符,用户会认为单元格为空。我想在任何回车符之前移动光标。
最佳答案
假设您使用的是VCL,InplaceEditor
是TCustomGrid
的一个属性。它的类型为 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/
我是一名优秀的程序员,十分优秀!