Recent

Author Topic: Cannot get file opening error to work properly  (Read 1361 times)

ranny

  • Jr. Member
  • **
  • Posts: 81
Cannot get file opening error to work properly
« on: September 26, 2023, 10:06:29 am »
Hi,

I have a program that needs to read a file before allowing any calculations to be carried out.  I also have a form and a message box popup with some details.   In my code, the file reading is carried out in the procedure "init" called by the form.Activate procedure.  In that "init" procedure I am forcing a "file not found" error however I get strange results.  The first object displayed is the warning of file not found with no main form1 displayed.  After acknowledging this, the main form appears and the message box saying "This program is used for...".  Acknowledge that and the main form disappears with the message box and the aboutbox never displays.  If I move the call to "init" into the for.Create procedure, it just goes round in circles showing the message boxes.  Hope the above explains the issue, where am I going wrong?  Thanks

 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2.     var
  3.     comdate:TDate;
  4.     dformat,s:string;
  5.     begin
  6.     //fill about box
  7.     recalculate;
  8.     s:=GetCompiledDate;
  9.     dformat:=formatsettings.ShortDateFormat;
  10.     formatsettings.ShortDateFormat:=('yyyy/mm/dd');
  11.     comdate:= StrtoDate(s);
  12.     formatsettings.shortdateFormat:=dformat;
  13.     abform.aboutbox.lblVer.caption:=GetFileVersion;
  14.     abform.aboutbox.lblCopy.caption:='Power company Ltd.';
  15.     abform.aboutbox.lblComp.Caption:=FormatDateTime('DD MMM YYYY',comdate)+' '+GetCompiledTime;
  16.     abform.aboutbox.lblCom.caption:='WARNING This program and its data are confidential.';
  17.     application.title:='Power Company '+GetFileVersion;
  18.     form1.caption:=application.title;
  19.     application.processmessages;
  20.  
  21.  
  22.  
  23.      messagedlg('This program is used for......',mtInformation,[mbok],0);
  24.  
  25.  
  26.     abform.aboutbox.showmodal;
  27.  
  28.     //initialise variables etc.
  29.     //init;
  30.  
  31.     end;

Code: Pascal  [Select][+][-]
  1.   if not FileExists('.\expdata.txt') then
  2.         begin
  3.         messagedlg(' Program cannot find the data file'+#10+'"expdata.hpp". Cannot continue.' ,mtError,[mbok],0);
  4.         application.terminate;
  5.         end;    

Josh

  • Hero Member
  • *****
  • Posts: 1344
Re: Cannot get file opening error to work properly
« Reply #1 on: September 26, 2023, 08:59:47 pm »
It sounds like form events are being fired when your not wanting them to

check out the order events are fired and when they are fired. It may help

https://wiki.freepascal.org/Event_order

it sounds as though the activate event is being fired several times, i suggest crating a global variable

Code: Pascal  [Select][+][-]
  1. Var fActivated:boolean=false;

and then in form activate event

Code: Pascal  [Select][+][-]
  1. begin
  2.   if Not FActivated then
  3.   begin
  4.      fActivated:=true; // makes sure the following code is only run once
  5.     init my stuf code goes here
  6.   end;
  7. end;
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 16196
  • Censorship about opinions does not belong here.
Re: Cannot get file opening error to work properly
« Reply #2 on: September 27, 2023, 09:08:53 am »
It is never a very good idea to manipulate the global record formatsettings directly. You should derive a local TFormatsettings for the form. The reason is that the global formatsettings derives from the OS system, so accidents will happen by default.
If I smell bad code it usually is bad code and that includes my own code.

ranny

  • Jr. Member
  • **
  • Posts: 81
Re: Cannot get file opening error to work properly
« Reply #3 on: September 27, 2023, 11:34:59 am »
Thanks for the comments.  I was mystified by the order of what was happening and it didn't make sense at all.  My understanding is the form.Create is first followed by form.Activate.  I used to have issues because I was putting stuff in form.Create that it couldn't manage as the form was not activated.  But I thought I had it correct here but still this strange issue of the sequence of events and that it was stuck in a loop.  Then I found this piece of information on a website.....

************
Normally an application is closed by closing the main form:

    Form1.Close;

However, this option is not always available (for example when an application has no form, or when you're working in a formless unit).

Option1:
   Application.Terminate;

Option2:
  Halt;

Option2 is probably the best option (although known to generate Access Violation when developing using Delphi 6 and running the application on Windows NT 4),... Option1 does not always seem to work (for example in exception handlers).
************


So I changed out Application.Terminate and put Halt in the code, and it worked as I wanted it to.  Interesting and welcoome any comments on this.

Ranny


Zvoni

  • Hero Member
  • *****
  • Posts: 2754
Re: Cannot get file opening error to work properly
« Reply #4 on: September 27, 2023, 11:48:20 am »
Don't use Activate for stuff like that.
You have that Code in Activate, you run your program, it works, on the side you open a WebBrowser, you surf to a site to check something (Browser having Focus!), you go back to your program, your code executes again!

Basically, what Josh said: Use a status-Variable
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

ranny

  • Jr. Member
  • **
  • Posts: 81
Re: Cannot get file opening error to work properly
« Reply #5 on: September 27, 2023, 02:03:04 pm »
Zvoni,  Interesting what you say about the code executing again when the form regains focus, and I have read that before, but strangely I have never had that happen in any of my programs.  Unsure why, but  I have many programs written with this upfront code and they all behave the same way without re-run of the code in the Activate event.

Zvoni

  • Hero Member
  • *****
  • Posts: 2754
Re: Cannot get file opening error to work properly
« Reply #6 on: September 27, 2023, 03:34:43 pm »
Zvoni,  Interesting what you say about the code executing again when the form regains focus, and I have read that before, but strangely I have never had that happen in any of my programs.  Unsure why, but  I have many programs written with this upfront code and they all behave the same way without re-run of the code in the Activate event.
Then you were just lucky, or you didn't notice it because it happened to fast
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 3647
Re: Cannot get file opening error to work properly
« Reply #7 on: September 27, 2023, 06:06:30 pm »
Then you were just lucky, or you didn't notice it because it happened to fast
Indeed. For TS: see also the documentation for OnActivate event

Quote
This handler is called when the form receives focus for the first time at application start up, and then subsequently each time focus is changed from another window for the same application to this window.
Ofc that will never (or better should not ever) happen when your application has only one window. So as stated by Zvoni you might have been lucky all this time.

I dislike spending another (form) variable on that so used another solution like posted here imho one of the rare occasions a typed constant is actually useful and without ambiguity.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

ranny

  • Jr. Member
  • **
  • Posts: 81
Re: Cannot get file opening error to work properly
« Reply #8 on: September 28, 2023, 08:45:56 am »
Thanks everyone for the information, it helps a lot to get more knowledge so that future projects will be improved.


 

TinyPortal © 2005-2018