这是我几年以前些的程序,看能否满足你的要求。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, Menus;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
MainMenu1: TMainMenu;
dhGL: TMenuItem;
dhHelp: TMenuItem;
dhAppend: TMenuItem;
dhExit: TMenuItem;
dhAbout: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure dhExitClick(Sender: TObject);
procedure dhAppendClick(Sender: TObject);
procedure dhAboutClick(Sender: TObject);
{procedure Button2Click(Sender: TObject);}
private
{ Private declarations }
public
{ Public declarations }
end;
Type D=Record
XM:String[28];
DH:String[18];
end;
Var
DH :File of D;
DhAdd:File of d;
Dm
;
SumNumber:Integer;//当前总电话的个数
Ydh:array[1..9999] of d;//该电话簿最大容量为9999个电话号码
Form1: TForm1;
implementation
uses dhAbout, addUnit2;
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
Var
I:Integer;
S:String;
begin
GetDir(0,s);
AssignFile(Dh,S+'/Dh.DAT');//电话数据文件名为Dh.DAT
AssignFile(DhAdd,S+'/DhAdd.DAT');//新增的电话数据文件名为DhAdd.DAT
Reset(dh);
ReSet(DhAdd);
I:=1;
With StringGrid1 Do
Begin
Cells[0,0]:=' 序号';
Cells[1,0]:=' 姓名';
Cells[2,0]:=' 电话号码';
ColWidths[0]:=40;
ColWidths[1]:=110;
ColWidths[2]:=130;
while not Eof(DhAdd) Do
begin
Read (DhAdd,Dm);
Cells[0,I]:=Format('%4d',
);
Cells[1,I]:=Format('%10s',[Trim(dm.xm)]) ;
Cells[2,I]:=Format('%10s',[Trim(Dm.dh)]) ;
Ydh.Xm :=Trim(dm.xm);
Ydh.Dh :=Trim(dm.dh);
Inc(I); // I:=I+1;
end;
While Not Eof(Dh) Do
Begin
Read(Dh,Dm);
Cells[0,I]:=Format('%4d',);
Cells[1,I]:=Format('%10s',[Trim(dm.xm)]) ;
Cells[2,I]:=Format('%10s',[Trim(Dm.dh)]) ;
Ydh.Xm :=Trim(dm.xm);
Ydh.Dh :=Trim(dm.dh);
Inc(I); //:=I+1;
End;
SumNumber:=I-1;
RowCount :=SumNumber+1;
end;
CloseFile(Dh);
CloseFile(DhAdd);
Form1.ActiveControl := Edit1;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
Var
I,N,Nmb:Integer;
S:String;
begin
With StringGrid1 Do
BEGIN
{如没有输入任何查询内容,则返回全部}
If Edit1.text='' Then
begin
for I :=1 to SumNumber do
begin
Cells[0,I]:=Format('%4d',);
Cells[1,I]:=Format('%10s',[Ydh.XM]) ;
Cells[2,I]:=Format('%10s',[Ydh.dh]) ;
end;
RowCount :=SumNumber+1;
end;
If (Key=#13) And (Edit1.Text<>'') Then
Begin
Nmb := StrToIntDef(Edit1.Text, 1010110452);
//***** 处理输入字符即输入姓名的情况 *********//
If Nmb=1010110452 Then
begin
S:=Edit1.Text;
N:=0;
Cols[0].Clear;
Cols[1].Clear;
Cols[2].Clear;
Cells[0,0]:=' 序号';
Cells[1,0]:=' 姓名';
Cells[2,0]:=' 电话号码';
for I := 1 to SumNumber do
begin
If Pos(S,Ydh.XM)>0 Then
begin
N:=N+1;
Cells[0,N]:=Format('%4d',[N]);
Cells[1,N]:=Format('%10s',[Ydh.XM]) ;
Cells[2,N]:=Format('%10s',[Ydh.dh]) ;
end;
end;
If n<1 then
begin
RowCount :=2;
Cells[1,1]:='没找到!' ;
Cells[2,1]:='';
end
else StringGrid1.RowCount :=N+1;
end;
//******* 处理输入数字的情况***************//
If Nmb<>1010110452 Then
begin
S:=Edit1.Text;
N:=0;
Cols[0].Clear;
Cols[1].Clear;
Cols[2].Clear;
Cells[0,0]:=' 序号';
Cells[1,0]:=' 姓名';
Cells[2,0]:=' 电话号码';
for I := 1 to SumNumber do
begin
If Pos(S,Ydh.DH)>0 Then
begin
N:=N+1;
Cells[0,N]:=Format('%4d',[N]);
Cells[1,N]:=Format('%10s',[Ydh.XM]) ;
Cells[2,N]:=Format('%10s',[Ydh.dh]) ;
end;
end;
If n<1 then
begin
RowCount :=2;
Cells[1,1]:='没找到!' ;
Cells[2,1]:='';
end
else StringGrid1.RowCount :=N+1;
end;
end;
END;
end;
procedure TForm1.dhExitClick(Sender: TObject);
begin
Close;
end;
procedure TForm1.dhAppendClick(Sender: TObject);
begin
frmAdd.ShowModal;
end;
{Type rD=Record
X:String[28];
H:String[18];
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
xDH :File of rD;
xDm:rD;
I:Integer;
begin
AssignFile(xDh,'xDh.DAT');//电话数据文件名为xDh.DAT
ReWrite(xdh);
for I:= 4 to SumNumber do
begin
xDm.X :=Ydh.XM;
xDm.H :=Ydh.DH;
Write(xDh,xDm);
end;
CloseFile(xDh);
end;}
procedure TForm1.dhAboutClick(Sender: TObject);
begin
AboutBox.ShowModal;
end;
end.