Forum > General
Cannot get file opening error to work properly
ranny:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormActivate(Sender: TObject); var comdate:TDate; dformat,s:string; begin //fill about box recalculate; s:=GetCompiledDate; dformat:=formatsettings.ShortDateFormat; formatsettings.ShortDateFormat:=('yyyy/mm/dd'); comdate:= StrtoDate(s); formatsettings.shortdateFormat:=dformat; abform.aboutbox.lblVer.caption:=GetFileVersion; abform.aboutbox.lblCopy.caption:='Power company Ltd.'; abform.aboutbox.lblComp.Caption:=FormatDateTime('DD MMM YYYY',comdate)+' '+GetCompiledTime; abform.aboutbox.lblCom.caption:='WARNING This program and its data are confidential.'; application.title:='Power Company '+GetFileVersion; form1.caption:=application.title; application.processmessages; messagedlg('This program is used for......',mtInformation,[mbok],0); abform.aboutbox.showmodal; //initialise variables etc. //init; end;
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if not FileExists('.\expdata.txt') then begin messagedlg(' Program cannot find the data file'+#10+'"expdata.hpp". Cannot continue.' ,mtError,[mbok],0); application.terminate; end;
Josh:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Var fActivated:boolean=false;
and then in form activate event
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---begin if Not FActivated then begin fActivated:=true; // makes sure the following code is only run once init my stuf code goes here end;end;
Thaddy:
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.
ranny:
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:
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
Navigation
[0] Message Index
[#] Next page