unit V4_1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) btnCreate: TButton; btnAppend: TButton; btnOpenRead: TButton; Memo1: TMemo; procedure btnCreateClick(Sender: TObject); procedure btnAppendClick(Sender: TObject); procedure btnOpenReadClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1;implementation{$R *.dfm}--创建文本文件 procedure TForm1.btnCreateClick(Sender: TObject); var MyTextFile: TextFile; s: string; i: integer; begin //关联文件 AssignFile(MyTextFile, 'MyTextFile.txt'); //覆盖或创建新文件 Rewrite(MyTextFile); //向文本文件里写字符串 try for i := 1 to 5 do begin s := 'This is line #'; Writeln(MyTextFile, s, i); end; finally CloseFile(MyTextFile); end; end;--添加内容 procedure TForm1.btnAppendClick(Sender: TObject); var MyTextFile: TextFile; s: string; i: integer; begin //关联文件 AssignFile(MyTextFile, 'MyTextFile.txt'); //判断文件是否存在 if FileExists('MyTextFile.txt') then //以添加的方式打开文本 Append(MyTextFile) else raise Exception.Create('MyTextFile.txt不存在'); //向文本添加字符串 try for i := 6 to 10 do begin s := 'This is line #'; Writeln(MyTextFile, s, i); end; finally CloseFile(MyTextFile); end; end; --读取内容 procedure TForm1.btnOpenReadClick(Sender: TObject); var MyTextFile: TextFile; s: string[14]; i, j: integer; begin //关联文件 AssignFile(MyTextFile, 'MyTextFile.txt'); //判断文件是否存在 if FileExists('MyTextFile.txt') then //以只读方式打开文本 Reset(MyTextFile) else raise Exception.Create('MyTextFile.txt不存在'); //读取文本数据 try while not eof(MyTextFile) do begin Readln(MyTextFile, s, j); Memo1.lines.Add(s + IntToStr(j)); end; finally CloseFile(MyTextFile); end; end; end.
读取内容应改为procedure TForm1.btnOpenReadClick(Sender: TObject); var MyTextFile: TextFile; s: string[14]; begin //关联文件 AssignFile(MyTextFile, 'MyTextFile.txt'); //判断文件是否存在 if FileExists('MyTextFile.txt') then //以只读方式打开文本 Reset(MyTextFile) else raise Exception.Create('MyTextFile.txt不存在'); //读取文本数据 try while not eof(MyTextFile) do begin Readln(MyTextFile, s); Memo1.lines.Add(s); end; finally CloseFile(MyTextFile); end; end;
读Edit中的内容可以写成:
String str;
str:=Edit.Text;
读Label控件的内容可以写成:
str:=Label.Caption;
读ListBox的内容可以写成:
str:=ListBox.Item[i]; //i为第几行,从0开始以此类推。
不好意思说一下,以后不要这么肉麻,我受不了,嘿嘿
string str;
读TStringList方法
TStringList List;
str := List.Strings[i];//第i+1行
读TListBox控件:
str := ListBox1.Item[i];//第i+1行
读TStrings内容:
str := Memo1.Lines.Strings[i];//第i+1行 读TMemo控件
str := RichEdit1.Items.Strings [i];//第i+1行 读TRichEdit控件
str := ComboBox1.Items.Strings [i];//第i+1行 读TComboBox控件
读TTreeNode内容:
str := TreeView1.Items.Item[0].Text;// 读控件TTreeView
谢谢各位兄长了!这不肉麻吧!呵呵!!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
btnCreate: TButton;
btnAppend: TButton;
btnOpenRead: TButton;
Memo1: TMemo;
procedure btnCreateClick(Sender: TObject);
procedure btnAppendClick(Sender: TObject);
procedure btnOpenReadClick(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;implementation{$R *.dfm}--创建文本文件
procedure TForm1.btnCreateClick(Sender: TObject);
var
MyTextFile: TextFile;
s: string;
i: integer;
begin
//关联文件
AssignFile(MyTextFile, 'MyTextFile.txt');
//覆盖或创建新文件
Rewrite(MyTextFile);
//向文本文件里写字符串
try
for i := 1 to 5 do
begin
s := 'This is line #';
Writeln(MyTextFile, s, i);
end;
finally
CloseFile(MyTextFile);
end;
end;--添加内容
procedure TForm1.btnAppendClick(Sender: TObject);
var
MyTextFile: TextFile;
s: string;
i: integer;
begin
//关联文件
AssignFile(MyTextFile, 'MyTextFile.txt');
//判断文件是否存在
if FileExists('MyTextFile.txt') then
//以添加的方式打开文本
Append(MyTextFile)
else
raise Exception.Create('MyTextFile.txt不存在');
//向文本添加字符串
try
for i := 6 to 10 do
begin
s := 'This is line #';
Writeln(MyTextFile, s, i);
end;
finally
CloseFile(MyTextFile);
end;
end;
--读取内容
procedure TForm1.btnOpenReadClick(Sender: TObject);
var
MyTextFile: TextFile;
s: string[14];
i, j: integer;
begin
//关联文件
AssignFile(MyTextFile, 'MyTextFile.txt');
//判断文件是否存在
if FileExists('MyTextFile.txt') then
//以只读方式打开文本
Reset(MyTextFile)
else
raise Exception.Create('MyTextFile.txt不存在');
//读取文本数据
try
while not eof(MyTextFile) do
begin
Readln(MyTextFile, s, j);
Memo1.lines.Add(s + IntToStr(j));
end;
finally
CloseFile(MyTextFile);
end;
end;
end.
var
MyTextFile: TextFile;
s: string[14];
begin
//关联文件
AssignFile(MyTextFile, 'MyTextFile.txt');
//判断文件是否存在
if FileExists('MyTextFile.txt') then
//以只读方式打开文本
Reset(MyTextFile)
else
raise Exception.Create('MyTextFile.txt不存在');
//读取文本数据
try
while not eof(MyTextFile) do
begin
Readln(MyTextFile, s);
Memo1.lines.Add(s);
end;
finally
CloseFile(MyTextFile);
end;
end;