【问题标题】:how can I insert a button in grid cell?如何在网格单元格中插入按钮?
【发布时间】:2014-09-04 09:59:25
【问题描述】:

如果您希望单元格显示按钮,则以下内容适用于 Delphi XE5。 但是,在 Delphi XE6 中却没有。

Type
    TSimpleLinkCell = class(TTextCell)
    protected
        FButton: TSpeedButton;
        procedure ButtonClick(Sender: TObject);
    public
        constructor Create(AOwner: TComponent); reintroduce;
    end;

constructor TSimpleLinkCell.Create(AOwner: TComponent);
begin
    inherited Create(AOwner);
    Self.TextAlign := TTextAlign.taLeading;
    FButton := TSpeedButton.Create(Self);
    FButton.Parent := Self;
    FButton.Height := 16;
    FButton.Width := 16;
    FButton.Align := TAlignLayout.alRight;
    FButton.OnClick := ButtonClick;
end;

如何在 Delphi XE6 中进行上述工作?

【问题讨论】:

  • 我已经测试了你的答案并且它有效。你能说这是正确的反应吗?请!

标签: delphi firemonkey delphi-xe6


【解决方案1】:
  1. 您的 SpeedButton 没有文本,因此在您使用鼠标进入按钮之前不会显示任何内容
  2. 如果你将这个对象插入到网格中的 TColumn 类型,它将起作用。这是您的代码的完整工作示例(在 XE4 上测试):

    unit Unit5;
    
    interface
    
    uses
      System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
      System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
      FMX.StdCtrls, FMX.Layouts, FMX.Grid;
    
    type
      TSimpleLinkCell = class(TTextCell)
      protected
          FButton: TSpeedButton;
          procedure ButtonClick(Sender: TObject);
       public
          constructor Create(AOwner: TComponent); reintroduce;
      end;
    
      TButtonColumn=class(TColumn)
      protected
        function CreateCellControl: TStyledControl;override;
      end;
    
      TForm5 = class(TForm)
        Grid1: TGrid;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form5: TForm5;
    
    implementation
    {$R *.fmx}
    
    constructor TSimpleLinkCell.Create(AOwner: TComponent);
    begin
        inherited Create(AOwner);
        Self.TextAlign := TTextAlign.taLeading;
        FButton := TSpeedButton.Create(Self);
        FButton.Parent := Self;
        FButton.Height := 16;
        FButton.Width := 16;
        FButton.Align := TAlignLayout.alRight;
        FButton.OnClick := ButtonClick;
    //    FButton.Text:='Button';
    end;
    
    procedure TSimpleLinkCell.ButtonClick(Sender: TObject);
    begin
      ShowMessage('The button is clicked!');
    end;
    
    function TButtonColumn.CreateCellControl: TStyledControl;
    var
      cell:TSimpleLinkCell;
    begin
      cell:=TSimpleLinkCell.Create(Self);
      Result:=cell;
    end;
    
    procedure TForm5.FormCreate(Sender: TObject);
    begin
      Grid1.AddObject(TButtonColumn.Create(Grid1));
    end;
    
    end.
    

【讨论】:

    猜你喜欢
    • 2020-02-04
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2015-09-15
    • 2021-12-30
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多