怎么样调用Delphi的含参过程 ( 积分: 50 )

  • 主题发起人 主题发起人 guogj0809
  • 开始时间 开始时间
G

guogj0809

Unregistered / Unconfirmed
GUEST, unregistred user!
Procedure LoadFromFile(const FileName: String);
begin

OpenDialog.Execute;

Memo1.lines.LoadFromFile(OpenDialog.FileName);

end;
应该在哪里写?
我在这里写的,不对。刚学,帮忙。
implementation

{$R *.dfm}
procedure loadfromfile(const filename:string);
begin
opendialog1.excute;
edit.text.loadfromfile(opendialog.filename);
end;
 
位置对的~~
1、写成窗体的过程
procedure tform1.loadfromfile(const filename:string);
begin
...
end;
2、写成单元的过程
procedure loadfromfile(const filename:string);
begin
...
//那么,这里使用的opendialog1、edit1估计你都是属于窗体的,则必须用
form1.opendialog1.excute;
form1.edit...
end;
 
跟含不含参无关,看样子你刚学,你看看下面的代码吧
unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure MyTest;
begin
form1.Edit1.Text := '111';
end;

procedure TForm1.MyTest1;
begin
Edit1.Text := '222';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MyTest1;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
MyTest;
end;

end.
 
Buddy.Sun:这样写和直接在窗体里加上opendialog,然后用opendialog的loadfromfile有什么区别?
 
谢谢,有点感觉了
 
Buddy.Sun:写成窗体和写成单元的用途有什么区别?麻烦给我说说
 
多人接受答案了。
 
睡醒了再说,现在别人都睡了,打字不方便
 
噢,不好意思,我在新疆,比你们晚2小时。
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses
unit2; //注意,不要漏了

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Test1;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
//test2是属于单元的方法,不属于 TForm2 或 TClsTest 任何一个类
unit2.Test2;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
test: TClsTest;
begin
test := TClsTest.Create;
try
test.Test3;
//与form2.Test1比较
//form2与test都是对象,只不过form2已经由程序自动创建(create)了,
//在project1单元,创建了from2对象(Application.CreateForm(TForm2, Form2);),
//所以可以直接使用form2,
finally
test.Free;
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
//类方法(方法前用class声明),可以直接用类的方式调用
TClsTest.Test4;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
//unit2.test5; //不可使用
//test5只能供unit2单元使用,因为它没有在单元实现(implementation)前对外声明
//test2就在前声明了
end;

end.
 
unit Unit2;

interface

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

type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
procedure Test1;
end;

TClsTest = class
public
procedure Test3;
class procedure Test4;
end;

procedure Test2;

var
Form2: TForm2;

implementation

{$R *.dfm}

procedure Test2;
begin
ShowMessage('2');
end;

procedure Test5;
begin
ShowMessage('5');
end;

{ TForm2 }

procedure TForm2.Test1;
begin
ShowMessage('1');
Test5;
end;

{ TClsTest }

procedure TClsTest.Test3;
begin
showmessage('3');
Test5;
end;

class procedure TClsTest.Test4;
begin
ShowMessage('4');
end;

end.

你体味下吧。。。
 
后退
顶部