如何得到加入点击事件?(15分)

  • 主题发起人 主题发起人 doglive
  • 开始时间 开始时间
D

doglive

Unregistered / Unconfirmed
GUEST, unregistred user!
我动态生成一个button,单击后输出提示,请问怎么写它的点击事件?
 
newbutton.onclick:=ButtonClick;
------------------------------
procedure TForm1.ButtonClick(Sender: TObject);
begin
//
end;
 
unit U_FormMian;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ExtCtrls,StdCtrls, ImgList;

type
TForm_Main = class(TForm)
ImageList1: TImageList;
procedure FormCreate(Sender: TObject);
procedure btClick(Sender: TObject);
procedure baClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form_Main: TForm_Main;
pa:TPanel;
pb:TPanel;
// pc:TPanel;
bt:Tbutton;
ba:Tbutton;
memo:TMemo;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm_Main.FormCreate(Sender: TObject);

begin
pa:=TPanel.Create(self);
//pa.Caption:='mubiaota';
pa.Left:=0;
pa.Top:=50;
pa.Width:=150;
pa.Height:=250;
pa.Align:=alLeft;
pa.Parent:=Self;

pb:=TPanel.Create(self);
pb.Left:=150;
pb.Width:=538;
pb.Align:=alTop;
pb.Parent:=self;

bt:=TButton.Create(self);
bt.Caption :='读取文件';
bt.Left:=23;
bt.Top:=20;
// bt.Width := 100;
//bt.Height := 50;
bt.Align := alNone;
bt.Parent :=pa;
bt.OnClick:= btClick;
// Memo1.Lines.Clear;

ba :=TButton.Create(self);
ba.Left:=23;
ba.Top:= 60;
ba.OnClick:= baClick;
ba.Caption:= '退出';
ba.Parent:=pa;

memo:=TMemo.Create(self);
memo.Align:=alClient;
memo.Parent:=pb;
//pb.Visible:=false;

//ComboBox1.Text:='请选择';


end;

procedure TForm_Main.btClick(Sender: TObject);
var
op:TOpenDialog;
str:string;
str1:TStringList;
i:integer;

//pb:TPanel;
begin
memo.Clear;

str1:=TStringList.Create;
op:=TOpenDialog.Create(self);


//op :=TOpenDialog.Create(self);
if op.Execute then
begin
str1.LoadFromFile(op.FileName);
for i:= 0 to str1.Count-1 do
begin

memo.Lines.Add(str1.Strings);

end;
end;

end;

procedure TForm_Main.baClick(Sender: TObject);
var
pForm:TForm2;
begin
pForm:=TForm2.Create(self);
//pForm.Parent:=TForm2;
pForm.Show;

end;

end.

全部是动态的 看看吧 很久不写程序了
 
procedure btnClick(sender: Tobject);
begin
// do something;
end;
var
btn: Tbutton;
begin
Btn := Tbutton.Create;
btn.parent := self;
btn.show;
btn.onclick := btnclick; // *****
end;

没测试.
 
如果我要向点击函数里面加入一个参数,加入后却发生错误,是不是不能加呀?
能的话应该怎么样做呢?
 
笨方法有一个,应该可以:
newbutton.onclick:=ButtonClick;
------------------------------
procedure TForm1.ButtonClick(Sender: TObject);
begin
参数 := 值 ;
//
end;
 
接受答案了.
 
后退
顶部