当我在 opendialog 中单击“取消”时,收到“i/o error 6”消息(我使用的是 delphi 11.3 ce)。 为什么会发生以及如何解决? 有代码示例,当“procedure TForm1.Button3Click”运行时会发生这种情况。 所有其他功能均正常工作 - 文件打开,文件中的文本显示。 另外,在开始时变量“文件名”定义了默认名称。
procedure TForm1.Button3Click(Sender: TObject);
begin
if state=false then begin \\examine if its need to save something before opening
if statef=false then begin SaveDialog1.Execute(); \\examine if its new file and save
FileName:=savedialog1.FileName;
AssignFile(opfile, FileName);
rewrite(opfile);
texty:=Memo1.Text;
write(opfile, texty);
closefile(opfile);
state:=true;
end
else if statef=true then begin statef:=true; \\examine if its opened file and save
AssignFile(opfile, FileName);
rewrite(opfile);
texty:=Memo1.Text;
write(opfile, texty);
closefile(opfile);
state:=true;
end;
end;
Memo1.Text:=''; \\clearing field
opendialog1.Execute(); \\opening file
FileName:=opendialog1.FileName;
AssignFile(opfile, FileName);
Reset(opfile);
while not Eof(opfile) do
begin
Readln(opfile, texty);
memo1.Lines.Add(texty); \\displaying text
end;
CloseFile(opfile);
state:=true; \\ return no saving needed
statef:=true; \\return file is opened already
end;
我想解决您的代码中的几个问题:
Execute
- Open/SaveDialog
的方法是一个返回布尔值的函数,指示对话框成功(选择或输入文件名)或中止。您根本不检查此结果,只需继续编写代码即可!要解决此问题,您需要在代码中添加另一个 if 块,如下所示:if OpenDialog.Exeute then
begin
// here all the stuff about handling the selected file
end;
if something = true
/ false
,只需 if x then
或 if not x then
就足够了。TMemo
来清除
MyMemo.Clear
if SaveDialog1.Execute then
begin
MyMemo.Strings.SaveToFile(SaveDialog1.FileName);
end;
或
if OpenDialog1.Execute then
begin
MyMemo.Strings.LoadFromFile(OpenDialog1.FileName);
end;
相反!