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

Delphi 12 FMX StringGrid单元格选中时无法自动全选内容的解决咨询

解决Delphi 12 FMX StringGrid选中单元格不自动全选内容的问题

Delphi 12对FMX StringGrid的编辑器默认行为做了调整,取消了选中单元格时自动全选内容的逻辑,需要通过自定义事件处理来恢复该功能。

基础解决方案:利用OnCreateEditor事件强制全选文本

给StringGrid添加OnCreateEditor事件处理,在编辑器创建后触发全选操作:

procedure TForm1.StringGrid1CreateEditor(Sender: TObject; const ACol, ARow: Integer;
  var AEditor: TStyledControl);
var
  Edit: TEdit;
begin
  if AEditor is TEdit then
  begin
    Edit := TEdit(AEditor);
    // 延迟执行确保编辑器初始化完成
    TThread.Queue(nil, procedure
    begin
      Edit.SelectAll;
    end);
  end;
end;

针对自定义编辑器的拓展处理

如果你的StringGrid列使用了TComboBox等自定义编辑器,需要获取其内部编辑控件再执行全选:

procedure TForm1.StringGrid1CreateEditor(Sender: TObject; const ACol, ARow: Integer;
  var AEditor: TStyledControl);
var
  ComboBox: TComboBox;
  Edit: TEdit;
begin
  if AEditor is TComboBox then
  begin
    ComboBox := TComboBox(AEditor);
    TThread.Queue(nil, procedure
    begin
      Edit := ComboBox.Edit;
      if Assigned(Edit) then
        Edit.SelectAll;
    end);
  end
  else if AEditor is TEdit then
  begin
    TThread.Queue(nil, procedure
    begin
      (AEditor as TEdit).SelectAll;
    end);
  end;
end;

关键说明

使用TThread.Queue是因为编辑器创建后可能尚未完成UI渲染,直接调用SelectAll可能无法生效,延迟到主线程队列执行能确保操作在编辑器就绪后触发。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:20:58