Un control común a disponer dentro de un formulario (TForm) son los botones, esto se hace disponiendo objetos de la clase TButton.
Confeccionar un formulario que muestre tres objetos de la clase TButton, disponer como etiqueta en cada botón los valores 1, 2 y 3. Cuando se presiona el botón mostrar en el título del formulario el valor de la etiqueta del botón presionado.
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Caption := Button1.Caption; end; procedure TForm1.Button2Click(Sender: TObject); begin Caption := Button2.Caption; end; procedure TForm1.Button3Click(Sender: TObject); begin Caption := Button3.Caption; end; end.
Debemos definir el evento OnClick para cada uno de los botones.
Cuando se presiona un botón modificamos la propiedad Caption del objeto Form1, como estamos dentro de un método de la clase TForm1 podemos acceder a las propiedades directamente por su nombre sin tener que anteceder el nombre del objeto Form1:
Caption := Button1.Caption;
Modificar el problema anterior para que se acumulen en el título del formulario los valores de los botones presionados.
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Caption := Caption + Button1.Caption; end; procedure TForm1.Button2Click(Sender: TObject); begin Caption := Caption + Button2.Caption; end; procedure TForm1.Button3Click(Sender: TObject); begin Caption := Caption + Button3.Caption; end; end.
Concatenamos el valor actual de la propiedad Caption del formulario con el valor de la propiedad Caption del botón respectivo:
procedure TForm1.Button1Click(Sender: TObject); begin Caption := Caption + Button1.Caption; end;
Modificar nuevamente el problema anterior para permitir mostrar hasta 10 caracteres en el título del formulario.
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin if Length(Caption) < 10 then Caption := Caption + Button1.Caption; end; procedure TForm1.Button2Click(Sender: TObject); begin if Length(Caption) < 10 then Caption := Caption + Button2.Caption; end; procedure TForm1.Button3Click(Sender: TObject); begin if Length(Caption) < 10 then Caption := Caption + Button3.Caption; end; end.
Para conocer la cantidad de caracteres que contiene la propiedad Caption que es de tipo String podemos utilizar la función Length:
procedure TForm1.Button1Click(Sender: TObject); begin if Length(Caption) < 10 then Caption := Caption + Button1.Caption; end;
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TForm1 = class(TForm) Label1: TLabel; Boton7: TButton; Boton8: TButton; Boton9: TButton; BotonDividir: TButton; Boton5: TButton; BotonPor: TButton; Boton6: TButton; Boton4: TButton; Boton2: TButton; BotonMenos: TButton; Boton3: TButton; Boton1: TButton; BotonPunto: TButton; BotonMas: TButton; BotonIgual: TButton; Boton0: TButton; procedure Boton0Click(Sender: TObject); procedure Boton1Click(Sender: TObject); procedure Boton2Click(Sender: TObject); procedure Boton3Click(Sender: TObject); procedure Boton4Click(Sender: TObject); procedure Boton5Click(Sender: TObject); procedure Boton6Click(Sender: TObject); procedure Boton7Click(Sender: TObject); procedure Boton8Click(Sender: TObject); procedure Boton9Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Boton0Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton0.Caption; end; procedure TForm1.Boton1Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton1.Caption; end; procedure TForm1.Boton2Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton2.Caption; end; procedure TForm1.Boton3Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton3.Caption; end; procedure TForm1.Boton4Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton4.Caption; end; procedure TForm1.Boton5Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton5.Caption; end; procedure TForm1.Boton6Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton6.Caption; end; procedure TForm1.Boton7Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton7.Caption; end; procedure TForm1.Boton8Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton8.Caption; end; procedure TForm1.Boton9Click(Sender: TObject); begin if Length(Label1.Caption) < 12 then Label1.Caption := Label1.Caption + Boton9.Caption; end; end.