Recent

Author Topic: تحرير المؤشّرات في OnClose أم OnDestroy  (Read 4506 times)

pascal111

  • Moderator
  • Sr. Member
  • *****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
تحرير المؤشّرات في OnClose أم OnDestroy
« on: August 29, 2021, 03:50:54 pm »
أيّهما أصح؟ تحرير المؤشرات في OnClose أم OnDestroy و مثال ذلكـ البرنامج التالي:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormDestroy(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.   x,y,z:^integer;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.  
  36.   new(x);
  37.   new(y);
  38.   new(z);
  39.  
  40.   x^:=100;
  41.   y^:=200;
  42.  
  43.   showmessage('x = '+inttostr(x^)+' ,y = '+inttostr(y^));
  44.  
  45.   z:=x;
  46.   x:=y;
  47.   y:=z;
  48.  
  49.   showmessage('x = '+inttostr(x^)+' ,y = '+inttostr(y^));
  50.  
  51. end;
  52.  
  53. procedure TForm1.FormDestroy(Sender: TObject);
  54. begin
  55.   dispose(x);
  56.   dispose(y);
  57.   dispose(z);
  58. end;
  59.  
  60. end.
  61.  
  62.  
La chose par la chose est rappelé.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: تحرير المؤشّرات في OnClose أم OnDestroy
« Reply #1 on: August 29, 2021, 04:39:12 pm »
The opposite of OnCreate is OnDestroy.
The opposite of OnShow is OnClose.

If the form is a mainform, closing the form will automatically call OnDestroy event.

But if it is not a mainform:
After a form is closed, it is not destroyed. It is still exist in the system memory.

When the program is running, a form (not the mainform) can be shown and closed multiple times. So if you want to set and reset default values for variables or objects, put them in OnShow and OnClose events.

But if you want to set default values for variables or objects and free them only once, put them in OnCreate and OnDestroy events.

If you want to learn more about form's events:
https://wiki.freepascal.org/Event_order#Forms

pascal111

  • Moderator
  • Sr. Member
  • *****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: تحرير المؤشّرات في OnClose أم OnDestroy
« Reply #2 on: August 29, 2021, 06:56:36 pm »
مُداخلة غير مُتوقّعة أيّها الزميل Handoko وأشكُركـ على المعلومات القيّمة فلقد فهمتُ هذه النُّقطة.
La chose par la chose est rappelé.

Zaher

  • Hero Member
  • *****
  • Posts: 679
    • parmaja.org
Re: تحرير المؤشّرات في OnClose أم OnDestroy
« Reply #3 on: September 19, 2021, 11:20:46 am »
اضف للفائدة، المتحولات لا تستفيد منها الا ضمن الفورم form ضعها داخل الفورم

Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     procedure FormCreate(Sender: TObject);
  3.     procedure FormDestroy(Sender: TObject);
  4.   private
  5.  
  6.   public
  7.      x,y,z:^integer;
  8.   .........
  9.  

 

TinyPortal © 2005-2018