Si, li puoi fare.
A sentimento sarebbe una cosa del genere ;)
var
buttons : array of TButton;
procedure TForm1.PopolaMenuDinamiciAllaVisualBasic;
Var
index : integer;
begin
setLength(buttons, 10);
for index :=low(buttons) to high(buttons) do
begin
Buttons[index] := TButton.create(self);
Buttons[index].onclick := buttonClick;
end;
procedure TForm1.ButtonsClick(Sender : TObject);
var
idx : integer;
begin
for index :=low(buttons) to high(buttons) do
begin
if (sender = buttons[idx]) then
begin
case idx of
0: fai qualche cosa
....
9 : siamo alla frutta
end;
end;
end;
end;
end;
Stilgar
Ciao
Ho provato il primo esempio; funziona ma č brutale!
Il secondo esempio mi piace di piu ma non sono riuscito ad implementarlo nel progetto di prova
Alla riga 16 del codice che ti posto si inchioda sulla dichiarazione dell procedura con il seguente errore:
unit1.pas(16,20) Fatal: Syntax error, ";" expected but "." found
L'unico '.' č quello tra TForm1 e Popolamenu
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
procedure Tform1.PopolaMenuDinamiciAllaVisualBasic;
procedure TForm1.ButtonsClick(Sender : TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
buttons : array of TButton;
implementation
{$R *.lfm}
procedure TForm1.PopolaMenuDinamiciAllaVisualBasic;
Var
index : integer;
begin
setLength(buttons, 10);
for index :=low(buttons) to high(buttons) do
begin
Buttons[index] := TButton.create(self);
Buttons[index].onclick := buttonsClick;
end;
procedure TForm1.ButtonsClick(Sender : TObject);
var
idx : integer;
begin
for index :=low(buttons) to high(buttons) do
begin
if (sender = buttons[idx]) then
begin
label1.caption :=str(idx);
{case idx of
0: fai qualche cosa
....
9 : siamo alla frutta
end; }
end;
end;
end;
end.
Sno agli inizi con il Pascal e Lazarus
ciao
Franco
emm .. il case non č scritto correttamente ...
L'ho buttato ad pene segugiorum ...
case idx of
0: Begin end;
1: Begin End;
2: Begin end;
// cosė via fino al
9: Begin End;
End;
;)
Stilgar
prova con le modifiche sotto
la dichiarazione della classe della form:
{$mode delphi}{$H+}
...
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
buttons : array of TButton;
procedure ButtonsClick(Sender : TObject);
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
button1->onclick
procedure TForm1.Button1Click(Sender: TObject);
Var
index : integer;
begin
setLength(buttons, 10);
for index :=low(buttons) to high(buttons) do
begin
Buttons[index] := TButton.create(self);
Buttons[index].onclick := TForm1.buttonsClick;
end;
end;
buttons click
procedure TForm1.ButtonsClick(Sender : TObject);
var
idx : integer;
begin
for idx :=low(buttons) to high(buttons) do
begin
if (sender = buttons[idx]) then
begin
label1.caption := inttostr(idx);
{case idx of
0: fai qualche cosa
....
9 : siamo alla frutta
end; }
end;
end;
end;
Ciao.
Adesso compila senza errori e va in esecuzione ma non visualizza i buttons. Si vede solo il form vuoto con Label1
Ho modificato la procedure di creazione dei button per impostare le proprietā. accludo il code
unit Unit1;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
// procedure Tform1.PopolaMenuDinamiciAllaVisualBasic;
procedure Button1Click(Sender : TObject);
private
{ private declarations }
buttons : array of TButton;
procedure ButtonsClick(Sender : TObject);
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
index : integer;
begin
setLength(buttons, 10);
for index :=low(buttons) to high(buttons) do
begin
Buttons[index] := TButton.create(self);
Buttons[index].onclick := TForm1.buttonsClick;
Buttons[index].Left:= 20 + 50*index;
Buttons[index].Top:=20;
Buttons[index].Height:=18;
Buttons[index].Width:=40;
Buttons[index].Visible:=true;
end;
end;
procedure TForm1.ButtonsClick(Sender : TObject);
var
idx : integer;
begin
for idx :=low(buttons) to high(buttons) do
begin
if (sender = buttons[idx]) then
begin
label1.caption :=inttostr(idx);
{case idx of
0: fai qualche cosa
....
9 : siamo alla frutta
end; }
end;
end;
end;
end.
NB. Se sostituisco delphi con objfpc segnala errore in compilazione
Franco
Ciao
I Buttons non apparivano perchč nessuno lanciava la procedura
procedure TForm1.Button1Click(Sender: TObject);
Ho aggiunto Button1 al form e cliccando appaiono i 10 button
Il problema č nella procedura
procedure TForm1.ButtonsClick(Sender : TObject);
che in un ciclo For dovrebbe identificare il button premuto e visualizzare in label1
il test
if (sender = buttons[idx]) then
provoca l'uscita immediata dal ciclo for
Ora Sender č un oggetto e anche Buttons[idx] č un oggetto
ma c'č qualcosa che non vā.
Accludo il codice della procedura
Franco
procedure TForm1.ButtonsClick(Sender : TObject);
var
idx : integer;
begin
for idx :=low(buttons) to high(buttons) do
begin
if (sender = buttons[idx]) then
begin
label1.caption :=inttostr(idx);
end;
end;
end;
secondo me l'indice non ti dovrebbe servire, puoi confrontare direttamente il puntatore
ad esempio:
if Sender = Button1 then
begin
end;
se non puoi fare a meno dell'indice, quando crei il bottone, memorizza l'indice nella proprietā Tag (del bottone stesso) e prova un approccio tipo:
procedure TForm1.ButtonsClick(Sender : TObject);
begin
label1.caption := IntToStr(TButton(Sender).Tag);
end;
in tutti e 2 i casi eviti il ciclo for ;)
Proteggi il codice scrivento "attorno"
try
except
on e: exception do
begin
end;
end;
poi una domanda, ma il callback ai bottoni l'hai impostato con @ o senza?
Se hai tolto delphi e messo objfpc le cose bambiano un pochino.
Stilgar
ho fatto delle prove, posto il mio codice
l'errore era nella riga con il commento
procedure TForm1.Button1Click(Sender: TObject);
Var
idx : integer;
begin
setLength(buttons, 10);
for idx :=low(buttons) to high(buttons) do
begin
Buttons[idx] := TButton.create(self);
Buttons[idx].OnClick := ButtonsClick; // := Tform1.ButtonsClick č errato
Buttons[idx].Top := 50 + (idx * 25);
Buttons[idx].Left := 220;
Buttons[idx].Caption := Format('Bottone nr. %d', [idx]);
Buttons[idx].Parent := self;
Buttons[idx].Tag := idx;
end;
end;
procedure TForm1.ButtonsClick(Sender : TObject);
begin
Label1.Caption := TButton(Sender).Caption + ' - Tag = ' + IntToStr(TButton(Sender).Tag);
end;
E' perfetto, grazie a tutti per l'aiuto. Ho modificato il programma inserendo la creazione dei button nell'vento FormCreate come suggeritomi da xinyiman
Ultima cosa. Le opzioni di compilazione delphi e objfpc
la quale provoca errori in compilazione. Potete spiegarmi il motivo e quale usare?
Franco
Accludo il programma modificato, per chi interessasse
unit Unit1;
{$mode delphi}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnClick(Sender: TObject);
private
Btn : Array of TButton;
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
//btn : Tbutton;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
ind : integer;
begin
setlength(Btn, 5);
for ind :=low(Btn) to high(Btn) do
begin
Btn[ind]:=TButton.Create (Self);
Btn[ind].Parent := Self;
btn[ind].Left:= 20 + 60*ind;
btn[ind].Top:=20;
btn[ind].caption:= 'btn' + IntToStr(ind);
btn[ind].Tag:=ind;
btn[ind].Name:='Btn' + IntToStr(ind);
btn[ind].onclick:=BtnClick;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.BtnClick(Sender: TObject);
var
buff : String;
begin
buff := Tbutton(sender).name;
label1.caption:=Tbutton(sender).caption;
label2.caption:= buff;
end;
end.