4

In Delphi 2010, I am creating a form, and then creating a TFrame, assigning TFrame.Parent to the form, and then showing the form MODALLY. Works fine... The frame has a DBNavigator, a field DBFields,etc. When the user clicks on Post/Save, I want to automatically close the Form. I have tried a few things, such as Close, Action = caFree, (DBNav.parent.parent) as TForm.Free, etc and nothing seems to work. How do I - from within a TFrame, close the Form?

Code to create this thing is...

 // Create the Window
    ThisWin := TEmptyFrameWin.Create(nil);

  // Create the Frame for the Window
  ThisFrame := TFrameUsage.Create(Application);

  ThisFrame.Parent := ThisWin;

  // Load the data
  ThisFrame.tUsage.Open;
  ThisFrame.tUsage.FindKey([StrToInt(ID)]);
  ThisFrame.LoadDateFields;

  ThisWin.Caption := 'Change Appointment Information';
  // Only show the POST button    
  ThisFrame.UsageNav.VisibleButtons := [sbPost];

  try
    ThisWin.ShowModal;
  finally
    ThisWin.Free;
  end;

Thanks,

GS

user1009073
  • 3,160
  • 7
  • 40
  • 82
  • 1
    Use `Release` instead of `Free` for subclasses of TForm. – Marcus Adams Sep 23 '13 at 18:03
  • 1
    There's no need for that here, @Marcus. `Release` is for when the form is being destroyed from within a message handler of the form or one of its controls. From the code shown here, it's impossible for the form to be processing any message at the time `Free` gets called, so everything is fine. – Rob Kennedy Sep 23 '13 at 19:04

1 Answers1

8

From a method within the frame class, you can reach the host form by calling GetParentForm. So, the following would meet your needs:

GetParentForm(Self).Close;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1 Didn't know this function. Implemented it several times myself for different projects. Thanks! – jpfollenius Sep 24 '13 at 06:38
  • This does what the OP asked, but if I was code-reviewing the OP's code I'd say "that's terrible". Make the frame have a `OnRequestClose` property/event (really a callback) and have the owner form decide whether to close, and what logic it wants to do before closing, and even whether to hook up the callback. – Warren P Sep 25 '13 at 01:33