Adebaldo
Adebaldo

Reputation: 21

how can I insert a button in grid cell?

The following works in Delphi XE5 if you want a cell to display a button. However, in Delphi XE6 it doesn't.

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;

How can i make the above work in Delphi XE6?

Upvotes: 1

Views: 3946

Answers (1)

LHristov
LHristov

Reputation: 1123

  1. Your SpeedButton has no text so nothing will be displayed until you went into the button with the mouse
  2. If you make TColumn type which inserted this object into the grid, it will work. Here is fully working example of your code (tested on 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.
    

Upvotes: 1

Related Questions