button1,button2,
Label1,Label2,Label3
按button1
label1 := 'ok';
label2 := 'no';
label3 := '1';
按button2
label1 := 'ok';
label2 := 'no';
label3 := '2';
现在上面label都有重复的地方,可以取出来共用
在其它语言我会使用函数来节省这些代码
但是在d中不知道如何写这个function?
请帮忙,看着电子书上的例子,不明白呀:(
Label1,Label2,Label3
按button1
label1 := 'ok';
label2 := 'no';
label3 := '1';
按button2
label1 := 'ok';
label2 := 'no';
label3 := '2';
现在上面label都有重复的地方,可以取出来共用
在其它语言我会使用函数来节省这些代码
但是在d中不知道如何写这个function?
请帮忙,看着电子书上的例子,不明白呀:(
begin
label1.Caption := 'ok';
label2.Caption := 'no';
label3.Caption := str;
end;
这样你就可以调用
按button1
proc('1');
按button2
proc('2');
Build
[Error] mainfrm.pas(36): Unsatisfied forward or external declaration: 'Tfrm.proc'
[Fatal Error] my.dpr(5): Could not compile used unit 'mainfrm.pas'
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
// 声明
procedure proc(str: string);
end;var
Form1: TForm1;implementation{$R *.dfm}
// 实现procedure TForm1.proc(str: string);
begin
label1.Caption := 'ok';
label2.Caption := 'no';
label3.Caption := str;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
proc('1');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
proc('2');
end;end.
:P
thx
begin
label1.Caption := 'ok';
label2.Caption := 'no';
if Sender = Button1 then
label3.Caption :='1'
else Sender = Button2 then
label3.Caption :='2';
end;然后在Button2的OnClick选Button1Click(在Inspecter中选)