Board index » delphi » One time event after create

One time event after create

I have many modal forms that are created and then destroyed after each
use.  While they are in use they can may call other modal forms.

I pass these forms some initialization values to their public variables
after I create them.  I want to avoid using global variables for these
values and don't like "cross-uses" statements where the called form also
"uses" the form that calls it.

If I use OnActivate to initialize the form it gets re-initialized when
the form becomes active again after the calling another modal form.

Is there an easy way to have something only run once other than having a
boolean variable that is set during OnActivate that disables this
function when it is run again or should I just do it as shown below?

private
  ActivateDone : boolean;

Procedure tForm1.FormCreate;
begin
  ActivateDone := false;  // Initializes to false during create but you
never know in the future.
end;

Procedure tForm1.FormActivate(sender : tObject);
begin
  if ActivateDone then exit;
  ActivateDone := true;
end;

...Jim

 

Re:One time event after create


Quote
"Jim Andrews" <j...@azdogs.com> wrote in message news:3B572E23.2476D7D8@azdogs.com...
> I have many modal forms that are created and then destroyed after each
> use.  While they are in use they can may call other modal forms.

> I pass these forms some initialization values to their public variables
> after I create them.  I want to avoid using global variables for these
> values and don't like "cross-uses" statements where the called form also
> "uses" the form that calls it.

> If I use OnActivate to initialize the form it gets re-initialized when
> the form becomes active again after the calling another modal form.

shouldn't be, unless you mean reuse after it has finished modal

Quote
> Is there an easy way to have something only run once other than having a
> boolean variable that is set during OnActivate that disables this
> function when it is run again or should I just do it as shown below?

 Procedure tForm1.FormActivate(sender : tObject);
 begin
     onactivate:=nil;
 end;

lets try a different way
[ just an excuse to show how to set up method procedures ;-)]

type
  TMyMethod = procedure of object;
  TForm2 = class(TForm)
  private
      Fmyactivate:TMyMethod;
      procedure myactivate;
  end;
procedure Tform2.myactivate;
begin
    Fmyactivate:=nil;
end;
procedure TForm2.FormActivate(Sender: TObject);
begin
    if assigned(FmyActivate) then FmyActivate;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
     FActivate:=myactivate;
end;

Re:One time event after create


Quote
In article <3B572E23.2476D...@azdogs.com>, Jim Andrews <j...@azdogs.com> writes:
>I pass these forms some initialization values to their public variables
>after I create them.  I want to avoid using global variables for these
>values and don't like "cross-uses" statements where the called form also
>"uses" the form that calls it.

>If I use OnActivate to initialize the form it gets re-initialized when
>the form becomes active again after the calling another modal form.

If the "modal" form is system modal then it would not bere-activated, but if it
is only application modal then it could be.

Quote
>Is there an easy way to have something only run once other than having a
>boolean variable that is set during OnActivate that disables this
>function when it is run again or should I just do it as shown below?

>private
>  ActivateDone : boolean;

>Procedure tForm1.FormCreate;
>begin
>  ActivateDone := false;  // Initializes to false during create but you
>never know in the future.

As I understand it, all object data is set to 0, 0.0000, nil, ''  (or whatever
zero is) when created.

If by a "global" variable (which you don't want to use) you mean ...

interface
var        <------- here
implementation
var

... then you could set a class variable which is ...

interface
var
implementation
var        <------- here

... and use a write only form property, which is set using a property method to
also maintain the class variable. The class .Create sets the initial private
property value from the class variable ...

  private
    { Private declarations }
    FMyInt : integer;
    procedure SetMyInt(AValue : integer);
  public
    property MyInt : integer write SetMyInt;
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

var
  MyIntInitial : integer;

procedure TForm2.FormCreate(Sender: TObject);
begin
  FMyInt := MyIntInitial; // FMyInt is used in other class methods
end;

procedure TForm2.SetMyInt(AValue : integer);
begin
  MyIntInitial := AValue;  // sets class variable
  if FMyInt <> AValue then  // usual other stuff
    FMyInt := AValue;
end;

Then to set the class variable from other forms ...

with TForm2.Create(self) do begin
  MyInt := 29;
  Free;
end;

 ... or set it the first time that form is used ...

  Form2 := TForm2.Create(Self);
  Form2.MyInt := 29;
  Form2.ShowModal;
  Form2.Free;

... and later uses would use the class variable value in the .Create method of
the class ...

  Form2 := TForm2.Create(Self);
  Form2.ShowModal;
  Form2.Free;

You could also use a record to hold all your settings to reduce coding;

I think its a bit messy - but it does what you want <g>.

Alan Lloyd
alangll...@aol.com

Re:One time event after create


Create your own AfterCreate event, e.g.

const
  wm_AfterCreate = wm_User;

type
  TForm1 = class(TForm)
    procedure WMAfterCreate(var Msg: TMessage); message wm_AfterCreate;
    procedure FormCreate(Sender: TObject);
  end;

[...]

procedure TForm1.FormCreate(Sender: TObject);
begin
  PostMessage(Handle, wm_AfterCreate, 0, 0);
end;

procedure TForm1.WMAfterCreate(var Msg: TMessage);
begin
  // Code goes here
end;

"Jim Andrews" <j...@azdogs.com> schreef in bericht
news:3B572E23.2476D7D8@azdogs.com...

Quote
> I have many modal forms that are created and then destroyed after each
> use.  While they are in use they can may call other modal forms.

> I pass these forms some initialization values to their public variables
> after I create them.  I want to avoid using global variables for these
> values and don't like "cross-uses" statements where the called form also
> "uses" the form that calls it.

> If I use OnActivate to initialize the form it gets re-initialized when
> the form becomes active again after the calling another modal form.

> Is there an easy way to have something only run once other than having a
> boolean variable that is set during OnActivate that disables this
> function when it is run again or should I just do it as shown below?

> private
>   ActivateDone : boolean;

> Procedure tForm1.FormCreate;
> begin
>   ActivateDone := false;  // Initializes to false during create but you
> never know in the future.
> end;

> Procedure tForm1.FormActivate(sender : tObject);
> begin
>   if ActivateDone then exit;
>   ActivateDone := true;
> end;

> ...Jim

Other Threads