unit Unit5;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, CheckLst;type
TlbForm = class(TForm)
CheckListBox1: TCheckListBox;
GroupBox: TGroupBox;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
//procedure CheckBox50Click(Sender: TObject);
procedure CheckBoxClick(Sender: TObject);
private
{ Private declarations }
arrcheckbox: array[1..21] of Tcheckbox;
public
{ Public declarations }
procedure checkboxarryclick(Sender: Tobject);
end;var
lbForm: TlbForm;
counter: integer;implementation{$R *.dfm}procedure TlbForm.FormShow(Sender: TObject);
begin
for counter:= 1 to 21 do
begin
arrcheckbox[counter]:=Tcheckbox.Create(self);
arrcheckbox[counter].Parent:=groupbox;
arrcheckbox[counter].Top:=counter*17;
arrcheckbox[counter].Left:=16;
end;
end;procedure TlbForm.FormCreate(Sender: TObject);
begin
arrcheckbox[counter].OnClick:= checkboxarryclick;
end;
procedure Tlbform.checkboxarryclick(sender: Tobject);
begin
showmessage('ok');
end;procedure TlbForm.CheckBoxClick(Sender: TObject);
begin
showmessage('fdsdfs');
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, CheckLst;type
TlbForm = class(TForm)
CheckListBox1: TCheckListBox;
GroupBox: TGroupBox;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
//procedure CheckBox50Click(Sender: TObject);
procedure CheckBoxClick(Sender: TObject);
private
{ Private declarations }
arrcheckbox: array[1..21] of Tcheckbox;
public
{ Public declarations }
procedure checkboxarryclick(Sender: Tobject);
end;var
lbForm: TlbForm;
counter: integer;implementation{$R *.dfm}procedure TlbForm.FormShow(Sender: TObject);
begin
for counter:= 1 to 21 do
begin
arrcheckbox[counter]:=Tcheckbox.Create(self);
arrcheckbox[counter].Parent:=groupbox;
arrcheckbox[counter].Top:=counter*17;
arrcheckbox[counter].Left:=16;
end;
end;procedure TlbForm.FormCreate(Sender: TObject);
begin
arrcheckbox[counter].OnClick:= checkboxarryclick;
end;
procedure Tlbform.checkboxarryclick(sender: Tobject);
begin
showmessage('ok');
end;procedure TlbForm.CheckBoxClick(Sender: TObject);
begin
showmessage('fdsdfs');
end;end.
begin
for counter:= 1 to 21 do
begin
arrcheckbox[counter]:=Tcheckbox.Create(self);
arrcheckbox[counter].Parent:=groupbox;
arrcheckbox[counter].Top:=counter*17;
arrcheckbox[counter].Left:=16;
arrcheckbox[counter].OnClick:= checkboxarryclick;
end;
end;
begin
arrcheckbox[counter].OnClick:= checkboxarryclick;
end;is wrong
formcreate happens before show do
so what is var counter's content?
you can uses rtti and attribute tag to reach your aim;
procedure TlbForm.FormCreate(Sender: TObject);
begin//arrcheckbox[counter]还没有被创建
arrcheckbox[counter].OnClick:= checkboxarryclick;
end;
一句应该放在TlbForm.FormShow中的for循环中。
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
private
arrcheckbox: array[1..21] of Tcheckbox;
{ Private declarations }
public
procedure checkboxarryclick(Sender: Tobject);
{ Public declarations }
end;var
Form1: TForm1;
counter: Integer;
implementation{$R *.dfm}procedure TForm1.checkboxarryclick(Sender: Tobject);
begin
if Sender = arrcheckbox[1] then//处理第一个复选框的代码
begin
ShowMessage('我是复选框1');
end;
if sender = arrcheckbox[2] then
begin
Close;
end;
end;procedure TForm1.FormShow(Sender: TObject);
begin
for counter:= 1 to 21 do
begin
arrcheckbox[counter]:=Tcheckbox.Create(self);
arrcheckbox[counter].Parent:=self;
arrcheckbox[counter].Top:=counter*17;
arrcheckbox[counter].Left:=16;
arrcheckbox[counter].Caption := '复选框' + IntToStr(counter);
arrcheckbox[counter].OnClick := checkboxarryclick;
end;
end;end.