Link to home
Start Free TrialLog in
Avatar of Ed Covney
Ed CovneyFlag for United States of America

asked on

Error Closing a Delphi Program

A program I'm writing requires data from a flash drive, so I inspect removable drives for the file. If there's a removable drive and the file exists, no problems. If there is no no file, I'd like to provide a message to the user to install his data flash drive then re-start the app. After he clicks OK, the program terminates but AFTER it ends, I get a error (from Windows?)  :
    EInvalidOperation with message 'Cannot change visible in OnShow or OnHide'

I don't use an OnShow or OnHide anywhere in the app yet the problem persists. I have installed the code in a new, blank form and it works fine. But when add it to the "OnActivate" event of my
program it bombs after termination.

Any help would is appreciated. Thanks.

Ed
SDataFile := LocateDataFile;   
             //Expecting like 'F:\DatFile.bin'

if SDataFile = '' then begin
   showmessage('Data File NOT found. '+
               'Please insert your data flash drive and re-start);
   Application.Terminate;      {'close' produces the same error}
End;

//Problems get much worse if I wait for the flash drive even
//when it gets inserted and the TDriveComboBox is updated.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ed Covney

ASKER

That was fast - thank you.

Based on the fact that we both got the code to work on a fresh new form, I changed the basic set-up in the OnActivate event of the form. instead of;
             If not filefound then close;
 I start with
             if filefound then begin
               {the remainder of the event code here}
               else begin
                 showmessage('Insert data drive');
                 close;
              end;

I do sorta remember from 30 years ago, that bad juju can happen if you try to quit in the middle.

Thank you for your Help Marqus
Glad you solved, but if the problem were that you think, should raise error even in a blank form, doesn't? I think there is something in LocateDataFile procedure or in some aother place that conflicts with Close statement. However, good bye
marqus -

The first thing I did was to transfer the code to a new form, where it worked as expected. And now that I changed the way / order of code execution, it also worlks in my program - using LocateDataFile.

I agree there may still be a problem somewhere and wish I knew something about error trapping which I've always been able to avoid. A trip to the bookstore is in my near future -:)

Thanks for sticking araound. - Ed
Check in the Delphi IDE menu:
Project-->Options
 In the Forms tab --> Autocreate forms:

Maybe you have some form that must not be there, then the form is autocreated and when you dont close o destroy that instance, then raise the error.