Objects (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example embeds a button to a TStringGrid cell using the Objects property. To test this example, add to the form a string grid object and a button. The FormCreate method creates a new instance of TButton, adds it on the form, and associates it to the first cell of the grid. Add the code from this method to the form's OnCreate event handler. Add the Button1Click method code to the OnPress event handler of the button. The result is that the grid is displayed. In the grid's first cell, the class of the object associated with it is also displayed.

Code

procedure TForm1.FormCreate(Sender: TObject);
var
   button: TButton;
begin

        button:=TButton.Create(Form1);//creates an instance of TButton
        //sets the coordinates of the button, where the button should appear on the form 
        button.Top:=StringGrid1.Height+10;
        button.Left:=10;
        button.Visible:=true;
        //sets the parent of the button
        button.Parent:=Form1;

        button.Caption:='cell[0,0]';
        //associate the new button with the first cell of the grid
        StringGrid1.Objects[0,0]:=button;

end;
procedure TForm1.Button1Click(Sender: TObject);
begin
   StringGrid1.Cells[0,0]:=StringGrid1.Objects[0,0].ClassName;
end;

Uses