Lazarus

Programming => General => Topic started by: AlphaInc. on September 16, 2020, 05:10:40 pm

Title: Process at application-start [SOLVED]
Post by: AlphaInc. on September 16, 2020, 05:10:40 pm
Hello everybody,

I want to implement a start-up process that, whenever the application get’s started, it checks for either a string in a file or a file itself, depends on what is easier to implement. If the string (or the file) is found then i want a button-caption to be set to On (for example) and if it’s not found than the Caption should be set to Off.
Either way, i don’t have the full path. I would like the application to look in the current directory (for example, in cmd it would be &CD&\folder1\file.ext

Unfortunately, I’m a total noob when it comes to developing and programming so i hope someone has the time to help me. 
Title: Re: Process at application-start
Post by: jamie on September 16, 2020, 10:42:25 pm
AButton.Enabled := FileExists('Your local file name');

Edit:

Or

Const Names:Array[Boolean] of String =('No', 'yes');

-------

 MyButton.Caption := names(FileExists('your Local File Name'));


Title: Re: Process at application-start
Post by: Bart on September 16, 2020, 11:09:35 pm
Assuming you are talking about a Lazarus application (you talk about a Button), you can put such code in the main forms OnCreate event.
Just double click on the form and an OnCreate event will be created for you.

Code: [Select]
procedure TForm1Create(Sender: TObject);
begin
  if FileExists('.\myfilename.extension') then
    Button1.Caption := 'Ok'
  else
    Button1.Caption := 'Not Ok';
end;

By starting the filename with '.\' the function FileExists will look for that file in the current directory (to be specific the directory from which the program was started, which can be different from the place where the application resides).

Bart
Title: Re: Process at application-start
Post by: AlphaInc. on September 17, 2020, 12:05:48 am
Thank you Bart, that was what I'm looking for.  ::)
TinyPortal © 2005-2018