问题描述
我有2个MDI ChildForms,Child1有一个TButton来打开Child2.我同时打开它没有任何问题,请禁用TButton,以防止Child2使用TButton重新创建.
I have 2 MDI ChildForms and the Child1 has a TButton to open the Child2. I do not have any issue opening it at the same time disable the TButton to prevent Child2 from recreating again using TButton.
现在,当我希望在关闭Child2时将Child1的TButton重新设置为启用"时,挑战就来了.
Now, the challenge comes when I want the TButton of Child1 back to "enabled" when I closed the Child2.
执行这些代码时出现访问错误:
I am getting access error when doing these code:
procedure TfrmChild2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
child1_u.frmChild1.btnOpenChild2Form.Enabled := True;
Action := caFree;
end;
我了解在处理MDI时有某种不同的方法.我在下面的运行时打开期间执行禁用TButton的代码时发现了这一点:
I understand there is somehow a different approach when dealing with MDI. I figured it out when I did the code for disabling the TButton during opening at runtime below:
procedure TfrmMain.btnOpenChild2(Sender: TObject);
begin
TfrmChild2.Create(frmMain);
btnOpenChild2.Enabled := False;
end;
但是要在关闭Child2表单时重新启用它是一个挑战.
But to enable it back when the Child2 form is closed is a challenge.
我试图在MainForm(所有者)中创建一个过程以触发在Child1中启用TButton:
I tried to create a procedure in the MainForm (Owner) to trigger the enable of TButton in the Child1:
procedure TfrmMain.EnableButtonAtChild1();
begin
child1_u.frmChild1.btnOpenChild1Form.Enabled := True;
end;
并在运行期间Child2的OnClose期间调用:
and called at runtime during OnClose of Child2:
procedure TfrmChild2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
EnableButtonAtChild1();
end;
我是MDI的新手,我需要了解在这种简单情况下访问组件的工作方式.在这里,我将不胜感激.
I am new to MDI and I need to understand how accessing components works particular this simple case. I will appreciate any help here.
推荐答案
我将采用另一种方法-当第一个孩子创建第二个孩子时,动态分配第二个孩子的 OnClose
事件.不要让第二个孩子尝试直接找到并访问第一个孩子:
I would take a different approach - assign the 2nd child's OnClose
event dynamically when the 1st child creates the 2nd child. Don't have the 2nd child try to find and access the 1st child directly:
procedure TfrmChild1.btnOpenChild2FormClick(Sender: TObject);
var
child: TfrmChild2;
begin
child := TfrmChild2.Create(Application.MainForm);
child.OnClose := Child2Closed;
btnOpenChild2Form.Enabled := False;
end;
procedure TfrmChild1.Child2Closed(Sender: TObject; var Action: TCloseAction);
begin
btnOpenChild2.Enabled := True;
Action := caFree;
end;
请确保在释放第一个孩子之前始终关闭第二个孩子,否则会遇到麻烦.如果需要,可以这样解决:
Just make sure the 2nd child is always closed before the 1st child is freed, otherwise you will have trouble. If you need to, you can solve that like this:
procedure TfrmChild1.FormDestroy(Sender: TObject);
var
I: Integer;
child: TForm;
event: TCloseEvent;
begin
for I := 0 to Application.MainForm.MDIChildCount-1 do
begin
child := Application.MainForm.MDIChildren[I];
event := child.OnClose;
if Assigned(event) and (TMethod(event).Data = Self) then
child.OnClose := nil;
end;
end;
这篇关于关闭另一个MDI子窗体后,启用MDI子窗体的TButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!