Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-01

Doing work in FormActivate

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gabor Boros

Hi,

Sometimes I used OnActivate event in Delphi for do some work and e.g. show a progressbar to the user. But with Lazarus this thing not working. Form showed up after the OnActivate finished. Is it bug http://bugs.freepascal.org/view.php?id=10654 . Can anybody have a good solution for this problem?

And and another question. Every time when the form activated the OnActivate event triggered, after switched to another app and switched back etc. I try to eliminate this by write if Active then Exit; to the OnActivate but in this case things after the if Activate... line never triggered.

Gabor

Reply by email

Doug Chamberlin
to Gabor Boros
Gabor Boros wrote 5 lines
Every time when the form activated the OnActivate event triggered, after switched to another app and switched back etc. I try to eliminate this by write if Active then Exit; to the OnActivate but in this case things after the if Activate... line never triggered.

For Windows the OnActivate event should fire every time the window is re-activated as you describe. This is as designed. If you want to process something only once when the window is first activated try the OnShow event.

If you must run some code only once but still within the OnActivate handler, the best way I have found is to add a field to the TForm that indicates the OnActivate has been run once. I usually use FActivated: Boolean for this. Then in the OnActivate handler put:

if not FActivated then begin
   //Do some work here that should only happen once
   FActivated := True;
   end;

Reply by email

Gabor Boros
to Doug Chamberlin

My main problem is the form not appears on the screen under OnActivate event. How can I make a progress form, display some informations, progress bars etc. and when the job finished the user can read the information and then close the form with a button?

Doug Chamberlin ?rta:

Show quoted text 14 lines
For Windows the OnActivate event should fire every time the window is re-activated as you describe. This is as designed. If you want to process something only once when the window is first activated try the OnShow event. If you must run some code only once but still within the OnActivate handler, the best way I have found is to add a field to the TForm that indicates the OnActivate has been run once. I usually use FActivated: Boolean for this. Then in the OnActivate handler put: if not FActivated then begin //Do some work here that should only happen once FActivated := True; end;

Reply by email

Benito van der Zander
Show quoted text 5 lines
For Windows the OnActivate event should fire every time the window is re-activated as you describe. This is as designed. If you want to process something only once when the window is first activated try the OnShow event.

However, this doesn't conform with the Delphi behaviour

Show quoted text 6 lines
If you must run some code only once but still within the OnActivate handler, the best way I have found is to add a field to the TForm that indicates the OnActivate has been run once. I usually use FActivated: Boolean for this. Then in the OnActivate handler put:
Easier is just to write OnActivate:=nil;

Benito

Reply by email

Doug Chamberlin
to Benito van der Zander
Benito van der Zander wrote 7 lines
> For Windows the OnActivate event should fire every time the window is > re-activated as you describe. This is as designed. If you want to > process something only once when the window is first activated try the > OnShow event. > However, this doesn't conform with the Delphi behaviour

You have confused me here. What did you mean by this?

As far as I can tell this is exactly how Delphi performs under Windows and exactly how it should perform. What am I missing?

Reply by email

Gabor Boros
to Doug Chamberlin

Hi,

A simple test case:

procedure TForm1.FormActivate(Sender: TObject);
begin
   Caption:=FormatDateTime('hh:mm:ss',Now);
end;

With Delphi the form's caption show the first activation time and with Lazarus the caption changed when switch to other application and switch back etc.. I don't say Delphi is correct but after working many years with Delphi this thing is strange for me.

Gabor

Doug Chamberlin ?rta:

You have confused me here. What did you mean by this? As far as I can tell this is exactly how Delphi performs under Windows and exactly how it should perform. What am I missing?

Reply by email

svaa
to Gabor Boros

Gabor Boros escribi?:

Show quoted text 15 lines
Hi, A simple test case: procedure TForm1.FormActivate(Sender: TObject); begin Caption:=FormatDateTime('hh:mm:ss',Now); end; With Delphi the form's caption show the first activation time and with Lazarus the caption changed when switch to other application and switch back etc.. I don't say Delphi is correct but after working many years with Delphi this thing is strange for me.

I think he is right, if you switch of application in delphi, the onActivate event is not fired.

Besides the first activate, the onActive event is fired when you show modal forms that are hidden, it's fired also when stayOnTop and mdiChild forms are unfocused and get the focus. But not when the switch of application (unless you bring to front a form when the application gets the focus again).

The onShow only works, besides the first activate, when you hide and show a form.

However, I have always found this behavior stranger or at least incomplete. I have always missed a "onFirstActivate" event. Most times I use onFormActivate to init some controls depending upon variables set by the caller between the create and the activate, most of this action must be done just one time, not each time it loses and gets the focus again. The problem is so usual that I have even made standard the use of a variable fFirstActivate that can be found is in many of my forms.

It wouldn't be a bad idea to add onFirstActivate although it breaks delphi compatibility a little.

Santiago A.

Reply by email

Lance Collins
to svaa

Several years ago I had a Windows app which ran happily on several sites. Then one of my customers installed a Citrix mainframe which emulated Windows somehow. Each time my app got a timeslice FormActivate fired. About 25 times a second as I vaguely recall.

I never use FormActivate these days.

I now used Loaded instead.

Lance

Reply by email