Destroing a form from its button. All created from code.

I have this:

var
  DisplayForm: TForm;
  CloseBtn: TButton;
begin
  DisplayForm := TForm.Create(nil);

  DisplayForm.Parent := nil;
  DisplayForm.Width := 500;
  DisplayForm.Height := 400;
  DisplayForm.Position := poDesktopCenter;
  DisplayForm.BorderStyle := bsSingle;
  DisplayForm.BorderIcons := [biSystemMenu];
  DisplayForm.Color := DisplayMessage.background_color;

  CloseBtn := TButton.Create(DisplayForm);
  CloseBtn.Parent := DisplayForm;
  CloseBtn.Left := 405;
  CloseBtn.Top := 330;
  CloseBtn.Caption := 'Close';

  CloseBtn.Visible := True;

  DisplayForm.Visible := True;
  CloseBtn.OnClick := AA;
end;

and in AA I have:

procedure TReminder.AA(Sender: TObject);
begin
  TForm(Sender).Parent.Destroy;
end;


What I`m trying to do is to destroy the form when I push the button! Can anyone help me ASAP ?

Thanks,
crystyanAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hmm would not really work so i guess

try this
var f : TForm;

if (sender is TWinControl) then
  if (TWinControl(sender).parent is Tform) then
  begin
    f :=  TWinControl(sender).parent as TForm;
    f.release;
  end;

on the other hand you could implement an onClose event of your TForm
with content

  action := caFree;

your buttonCode looks then like

if (sender is TWinControl) then
  if (TWinControl(sender).parent is Tform) then
  begin
    f :=  TWinControl(sender).parent as TForm;
    f.close;
  end;

 meikl ;-)
0
 
kretzschmarCommented:
replace
 TForm(Sender).Parent.Destroy;
with
 TForm(Sender).Parent.Release;

meikl ;-)
0
 
crystyanAuthor Commented:
It works ace !!!
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.