delphi中如何调用函数过程

sadasfaff 2015-01-16 10:46:17
unit button;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

function GetFileCount(srcPath, srcFileName: string): Integer;


var
FileRec: TSearchrec;
currPath: string;

begin
if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\';
currPath := srcPath + '*.*';
Result := 0;
if FindFirst(currPath, faAnyFile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) and
(FileRec.Name <> '.') and
(FileRec.Name <> '..') then
begin
Result := Result + GetFileCount(srcPath + FileRec.Name, srcFileName);
end else
if AnsiCompareText(srcFileName, FileRec.Name) = 0 then
Result := Result + 1;
until FindNext(FileRec) <> 0;
end;
begin
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

Button1.Caption :=function GetFileCount;

end;

procedure TForm1.Label1Click(Sender: TObject);
begin
label1.caption:='333333'
end;

end.
在button上如何调用GetFileCount这个函数过程
...全文
418 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
看点书吧!!!!!!!!!!!!!!
sadasfaff 2015-01-16
  • 打赏
  • 举报
回复
召唤大神啊啊啊 啊啊
sadasfaff 2015-01-16
  • 打赏
  • 举报
回复
来人啊啊啊啊
lyhoo163 2015-01-16
  • 打赏
  • 举报
回复
编译,验证时,使用一个U盘。在C:\盘目录文件太多,会执行很长时间。 U盘的不同的目录,拷贝若干个1.txt文件。 就可以验证了。比如U盘是I:
procedure TForm1.Button1Click(Sender: TObject);
var n:integer;
begin
  n:=GetFileCount('I:', '1.txt');
  showmessage('有'+inttostr(n)+'个同名文件');
end;
lyhoo163 2015-01-16
  • 打赏
  • 举报
回复
错误: function GetFileCount(srcPath, srcFileName: string): Integer; 定义在FormCreat事件 中, Button事件是不能调用的。 修正: 你应该将它拉出来,放在 private 或 public { Public declarati 均可: 单元代码:
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 }
    function GetFileCount(srcPath, srcFileName: string): Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetFileCount(srcPath, srcFileName: string): Integer;
var FileRec: TSearchrec;
    currPath: string;
begin
  if srcPath[Length(srcPath)] <> '\' then srcPath := srcPath + '\';
  currPath := srcPath + '*.*';
  Result := 0;
  if FindFirst(currPath, faAnyFile, FileRec) = 0 then
  repeat
    if ((FileRec.Attr and faDirectory) <> 0) and
      (FileRec.Name <> '.') and
      (FileRec.Name <> '..') then
    begin
      Result := Result + GetFileCount(srcPath + FileRec.Name, srcFileName);
    end
    else
      if AnsiCompareText(srcFileName, FileRec.Name) = 0 then
        Result := Result + 1;
  until FindNext(FileRec) <> 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var n:integer;
begin
  n:=GetFileCount('c:', 'cmd.exe');
  showmessage('有'+inttostr(n)+'个同名文件');
end;

end.
不得闲 2015-01-16
  • 打赏
  • 举报
回复
不明觉厉,直接调用不就好了么,感觉好像是来搅乱的。。。 procedure TForm1.Button1Click(Sender: TObject); begin Button1.Caption :=function GetFileCount; end; 改成 procedure TForm1.Button1Click(Sender: TObject); begin Button1.Caption :=inttostr(GetFileCount); end;

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧