我有两种形式,Form1和Form2。我从Form1打开Form2。 我在Form2中有3个按钮。好的,取消并加载。我已经为OK按钮设置了mrOK,为取消按钮设置了mrCancel,为了加载按钮设置了mrNone。 当我单击“加载”时,我想在表单中加载项目列表。但是,当我单击“加载”按钮时,项目已加载但表单立即关闭。
这是守则。 在Form1中,
procedure TForm1.Configure
var
TheForm : TForm2;
reply : TModalResult;
begin
try
// Some logic
Reply := TheForm.showModal;
if Reply = mrOk then
begin
//Some logic
end;
//Some more smts
finally
FreeAndNil(TheForm);
end;
end;
在Form2中,LoadClick按钮事件有一些逻辑。 Form2 Logic
procedure TForm2.btnLoadClick(Sender: TObject);
var
Alist : TStringList;
begin
if DirectoryExists(DataDir) then
begin
try
for i:=1 to cboList.Items.Count -1 do
begin
Response := MessageDlg('Do you want to insert into the list', mtConfirmation,[mbYes,mbNo], 0);
if Response = mrYes then
begin
InsertNewItem(cboList.Items[i]);
end;
if Response = mrNo then
begin
MessageDlg('Not added to the list', mtWarning , [mbOK], 0);
end;
end;
finally
AList.Free;
end;
end;
执行所有逻辑但是在输入finally语句后单击“加载”按钮后关闭表单。我可以保持表单关闭,直到我单击确定或取消?
如果我单击表单2中的“确定”按钮,我需要在Form1中执行必要的操作。因此,我不想将结束部分移动到OK按钮单击事件。
你可以在这个上建议我吗?谢谢您的帮助。答案 0 :(得分:2)
在CodeRage 9中搜索我的视频"您是否接受了内部软件管道工?"深入讨论这个一般性主题。
基本上,您需要在Form2中定义OnClose
处理程序,设置Action := caHide;
,否则默认设置为caFree
,这不是您想要的
另外,如果您不打算再次使用本地变量,那么您不需要FreeAndNil
。 TheForm.Free;
就足够了。