Recent

Author Topic: [SOLVED] Placing Opening Code in EXE for Opening Associate Files  (Read 3943 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #15 on: July 13, 2019, 03:54:08 am »
FYI, when I upload this updated version tomorrow, It will have a messagebox (see screen below) that gives you credit.

It's not really necessary, just helping is a pleasure for me. But if you do, note that my name is "Luis Caballero", not "Luis Martínez"

Although just "lucamar" is good enough :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #16 on: July 13, 2019, 08:40:45 am »
Okay.... I''ll change the credit :)
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

Ally

  • Jr. Member
  • **
  • Posts: 81
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #17 on: July 13, 2019, 12:36:50 pm »
Hello lucamar,

you have written:

The problem with that code is that the OnShow event is trigered on many conditions, not just on starting the program, so you keep (re-)loading the parameters at odd moments during the application run-time.

For my programs, FormShow will only fire once when the program starts. Under what conditions can FormShow be executed while the program is running?

Greeting Roland

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #18 on: July 13, 2019, 02:05:15 pm »
For the main form of a simple app, FormShow is normally executed only once. For all other forms which can be opened and closed at will without terminating the app, every time a closed form is opened a FormShow event is fired for the form that is now showing.

[Edited to take account of lucamar's later contribution: the main form of an app can clearly have OnShow event called more than once. This is probably the exception rather than the rule, however.]
« Last Edit: July 13, 2019, 08:24:56 pm by howardpc »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #19 on: July 13, 2019, 03:32:17 pm »
For the main form of the app, FormShow is executed only once.

That depends on whether that form is always showing or not. Try this (rather contrived) example: Create a new app (call it "peek-a-boo", for example :)) and add a button, a timer (Interval: 5000) an a memo to the form. Then add these handlers to it:
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.btDoTestClick(Sender: TObject);
  2. begin
  3.   Timer.Enabled := True;
  4.   Hide;
  5. end;
  6.  
  7. procedure TMainForm.TimerTimer(Sender: TObject);
  8. begin
  9.   Timer.Enabled := False;
  10.   Show;
  11. end;
  12.  
  13. procedure TMainForm.FormShow(Sender: TObject);
  14. begin
  15.   Memo.Lines.Add('OnShow event triggered');
  16. end;

Run and enjoy :)

I don't remember exactly all the conditions in which OnShow is triggered but I did some tests some time ago and it did get called more than once. Not constantly, of course, but in enough circumstances as to merit the precaution.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Ally

  • Jr. Member
  • **
  • Posts: 81
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #20 on: July 13, 2019, 06:00:52 pm »
Hello howardpc,
that's how I see it, and since it's about the program start and MainForm, there should not be any problems.

Hello lucamar,
In your example, the running program calls its own FormShow. I do not think that usually does.
Are there any situations in which TMainForm.FormShow is executed without explicitly calling it?

Greeting Roland

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #21 on: July 13, 2019, 06:38:22 pm »
Hello lucamar,
In your example, the running program calls its own FormShow. I do not think that usually does.
Are there any situations in which TMainForm.FormShow is executed without explicitly calling it?

As I said, I don't remember all the conditions in which it's called. There were not many but it was a long time ago; it should be easy to find them: grep the sources of (at least) the forms units for constructs of the form "if Assigned(FOnShow) then".

My example above is contrived as h**l; it's intention is to demo one way in which OnShow can be triggered more than once for the main form--but note that I don't explicitely call the event. It gets called as a result of calling Hide/Show.

Note also that there are actually quite a lot of applications in which the function of the main form is to act as a kind of "junction box" for all the forms. Such applications may show/hide the main form (and all the others) quite a lot of times. Granted that it's not what we could term "normal" but the possibility is there; why then would not one take the two seconds it takes to implement the guard even if it'll never be needed?
« Last Edit: July 13, 2019, 06:44:13 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Ally

  • Jr. Member
  • **
  • Posts: 81
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #22 on: July 14, 2019, 10:18:17 am »
Hello lucamar,

I found something else. Also Visible: = False; > Visible: = True; calls FormShow.
Your tip, to install a guard here, can be quite helpful.
Thank you for your answers.

Greeting Roland

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: [SOLVED] Placing Opening Code in EXE for Opening Associate Files
« Reply #23 on: July 14, 2019, 10:31:34 am »
I found something else. Also Visible: = False; > Visible: = True; calls FormShow.

That's quite logical. I haven't looked but I bet the implementation of the setter is something similar to
Code: Pascal  [Select][+][-]
  1. procedure TCustomForm.SetVisible(Value: Boolean);
  2. begin
  3.   if Value = FVisible then Exit;
  4.   FVisible := Value;
  5.   if  FVisible then
  6.     Show
  7.   else
  8.     Hide;
  9. end;
;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018