我在form1中定议了一个过程,在form2中调用它时出错,无法调用。(20分)

  • 主题发起人 主题发起人 chuanxue
  • 开始时间 开始时间
C

chuanxue

Unregistered / Unconfirmed
GUEST, unregistred user!
我在form1中定议了一个过程,在form2中调用它时出错,无法调用。清各位帮我看看如何调用
 
代码呢?
 
检查function声明是否为public

form1.过程名 就可以了,当然出错也有可能是过程内部代码的问题
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure hello;
begin
showmessage('hello');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
hello;
end;

end.





下面是在form2中:
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.hello;
end;

end.
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
procedure hello;//定义为全局函数

implementation

{$R *.dfm}
procedure hello;
begin
showmessage('hello');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
hello;
end;

end.





下面是在form2中:
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
hello;
end;

end.
 
或者:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure hello;
end;

var
Form1: TForm1;


implementation

{$R *.dfm}
procedure TForm1.hello;
begin
showmessage('hello');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
hello;
end;

end.





下面是在form2中:
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}
uses
Unit1;

procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.hello;
end;

end.
 
procedure Button1Click(Sender: TObject);
procedure hello;

procedure hello; //??????????????
procedure TForm1.hello; //Right
begin
showmessage('hello');
end;
 
定义为public过程 才能调用
 
unit2 中加入要引用的单元文件名unit1;
 
Form1的声明中加入
procedure hello;
过程名改为:
procedure TForm1.hello;
 
已解决,谢谢各位
 
后退
顶部