button1,button2,
Label1,Label2,Label3
按button1
 label1 := 'ok';
 label2 := 'no';
 label3 := '1';
按button2
 label1 := 'ok';
 label2 := 'no';
 label3 := '2';
现在上面label都有重复的地方,可以取出来共用
在其它语言我会使用函数来节省这些代码
但是在d中不知道如何写这个function?
请帮忙,看着电子书上的例子,不明白呀:(

解决方案 »

  1.   

    procedure proc(str: string);
    begin
      label1.Caption := 'ok';
      label2.Caption := 'no';
      label3.Caption := str;
    end;
    这样你就可以调用
    按button1
    proc('1');
    按button2
    proc('2');
      

  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'
      

  3.   

    // 看来你接触Delphi不久,给你个全的:
    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.
      

  4.   

    哇,原来需要在public那里定义的啊,呵呵,前面两个都试了,就这个没试
    :P
    thx
      

  5.   

    还可以是这样,更好:procedure TForm1.Button1Click(Sender: TObject);
    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中选)