You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FMX中是否存在与TStringGrid::SetEditText功能等价的方法?

FMX网格实时输入更新的替代实现方案

VCL中的SetEditText方法在FMX框架下没有直接同名API,可通过以下两种成熟方案实现相同的实时监听/修改用户输入的效果:

方案1:使用官方自带事件(无需自定义控件)

适配FMX标准TStringGrid、TGrid控件,直接绑定OnEditing事件即可实现实时更新:

  • 触发时机:用户进入单元格编辑状态后,每输入一个字符都会触发该事件
  • 实现代码示例:
procedure TForm1.StringGrid1Editing(Sender: TObject; ACol, ARow: Integer; var CanEdit: Boolean);
var
  CurrentEditor: TCustomEdit;
  InputValue: Double;
begin
  // 获取当前正在编辑的输入框实例
  if StringGrid1.Editor is TCustomEdit then
  begin
    CurrentEditor := TCustomEdit(StringGrid1.Editor);
    // 读取实时输入内容,执行业务计算逻辑
    if TryStrToFloat(CurrentEditor.Text, InputValue) then
    begin
      // 示例:实时更新相邻单元格的计算结果
      StringGrid1.Cells[ACol + 1, ARow] := FloatToStr(InputValue * 2.3);
      // 如果需要修改当前输入框的内容,直接赋值即可
      // CurrentEditor.Text := Format('%.2f', [InputValue]);
    end;
  end;
end;

方案2:重写编辑控件绑定事件(更高可控性)

如果需要更精细的输入控制,或者使用自定义网格控件,可通过重写网格的CreateEditor方法,给编辑控件绑定OnChangeTracking事件实现:

type
  TCustomStringGrid = class(TStringGrid)
  protected
    function CreateEditor: TFMXObject; override;
    procedure EditorInputChanged(Sender: TObject);
  end;

function TCustomStringGrid.CreateEditor: TFMXObject;
begin
  Result := inherited CreateEditor;
  // 给生成的编辑控件绑定实时变化事件
  if Result is TCustomEdit then
    TCustomEdit(Result).OnChangeTracking := EditorInputChanged;
end;

procedure TCustomStringGrid.EditorInputChanged(Sender: TObject);
var
  InputText: string;
begin
  InputText := TCustomEdit(Sender).Text;
  // 此处写你需要的实时更新逻辑,效果完全等价于VCL的SetEditText
  // 支持修改输入内容、更新其他单元格数值、触发校验逻辑等操作
end;

补充说明:如果需要强制修改当前正在输入的内容,直接对TCustomEdit实例的Text属性赋值即可,FMX会自动保留光标位置,不会打断用户的输入操作。

内容的提问来源于stack exchange,提问作者LightFox7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 13:36:02