0

I know this has been raised previously but that answered question suggested different approach and it did really works for me.

Here's the link Enable a TButton of an MDI ChildForm upon closing of another MDI ChildForm.

However and again, I encountered this same simple challenge in another scenario.

I created a TButton in TForm1 to open TForm2 at the same time during opening it should disable the TButton to avoid recreation or reopening of TForm2.

By the way, I am using MDI here. Both forms I mentioned above are child forms.

Now, I wanted to enable back the TButton of TForm1 at the time of closing the TForm2 but I am getting an 'Access Violation Error'.

Here's the code I got for closing:

procedure TfrmChild2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  form1_u.frmChild1.btnOpen.Enabled := True;        //   1. here's where the error code is coming
  main_u.frmParent.menuitemOpen.Enabled := true;    //   2. this one form main form has no problem
end;

I am surprised the second procedure above is okay, though its triggered from the MDI parent form, while the other child form isn't and it is throwing 'access violation error'.

Juke
  • 135
  • 2
  • 9
  • 1
    For that error to pop, one of these (form1_u, frmChild1 or btnOpen) is nil at the time of execution. put a break point at this line and when the debugger reaches this point, hover with your mouse to see witch one is nil. If none of them is nil, trace the Enabled property and check what happens when you set it. – Nasreddine Galfout Jan 24 '20 at 23:32
  • The [solution I gave you earlier](https://stackoverflow.com/a/59779825/65863) applies to this situation, too. You have stated that it works, so why would you not want to continue using it? When the `TButton` creates the new `Form2` object, assign an `OnClose` handler to that object, and let the handler re-enable the `TButton` using the `Self` pointer of the event handler to reach the `TButton`. – Remy Lebeau Jan 24 '20 at 23:39
  • @RemyLebeau I tried your solution in this case but my 'Action := caFree;' did not trigger. – Juke Jan 25 '20 at 00:57
  • 1
    @Juke then please [edit] your question to show all of the code that is not working for you. `OnClose` and `Action=caFree` work fine for MDI child Forms when used correctly. – Remy Lebeau Jan 25 '20 at 02:33

0 Answers0