1. Don't make it public if it is not needed public
counter : byte;
end;
Above is your code. Do you have any reason why the counter is public? It is usually a good practice to set a property or variable to the lowest visibility. So instead of making it public, it is better to make it private:
private
counter : byte;
end;
2. Replace counter with a boolean variableBecause there are only 2 states: on and off, it will be better to use a boolean-typed variable. Also I rename the counter to isRemastered, which is more meaningful. So the variable counter now becomes:
private
isRemastered: Boolean;
end;
3. Change the declaration of TInfo1Because we now use boolean instead of byte, we need to change the declaration of TInfo1:
type
TInfo1 = array[Boolean] of Trec;
const
Version: TInfo1 = ((cap:'Off.'; msg:'This is remastered version'),
(cap:'On.'; msg:'This is original version'));
Also replace the texts for cap and msg to whatever you want.
4. Button1.OnClick should call VersionCheckIt should call VersionCheck, which will update the caption of the button:
procedure TForm1.Button1Click(Sender: TObject);
begin
if isRemastered then
begin
// Do something if currenct version is remastered
//ShellExecute(...
end
else
begin
// Do something is currenct version is not remastered
//ShellExecute(...
end;
VersionCheck;
Memo1.Lines.Add(Version[isRemastered].msg);
end;
5. VersionCheck need to be called when program startsYour Form1.FormCreate should call VersionCheck, which will set the value for isRemastered and update the button's caption.
procedure TForm1.FormCreate(Sender: TObject);
begin
VersionCheck;
end;
6. The VersionCheck procedureYour VersionCheck procedure should be something like this:
procedure TForm1.VersionCheck;
var
MyFile: Text;
Line1: string;
begin
// Read the config file
AssignFile(MyFile, 'config.txt');
Reset(MyFile);
ReadLn(MyFile, Line1);
CloseFile(MyFile);
// Set the value for isRemastered
isRemastered := (Line1 = 'version = remastered');
// Set the button's caption
Button1.Caption := Version[isRemastered].cap;
end;
For your information, you set the Line1's value by checking the 'version = remastered' in the config file is not a good practice. The better solution is using TIniFile. Maybe you're not ready to try TIniFile, but remember to study it if you have time.
https://wiki.freepascal.org/Using_INI_Files7. BonusFollowing the all 6 steps above, the code should work now. But your code for file reading can cause a serious issue if something bad happens when reading the file.
Below is try-except block added to VersionCheck procedure:
procedure TForm1.VersionCheck;
var
MyFile: Text;
Line1: string;
begin
// Load isRemastered's value from config file
AssignFile(MyFile, 'config.txt');
try
Reset(MyFile);
ReadLn(MyFile, Line1);
CloseFile(MyFile);
isRemastered := (Line1 = 'version = remastered');
except
ShowMessage('Error reading file');
isRemastered := False; // Set the default value if reading error
end;
// Set the button's caption
Button1.Caption := Version[isRemastered].cap;
end;
If you're not familiar with exception block you can use IOResult.
Read more:
https://wiki.freepascal.org/Exceptionshttps://www.freepascal.org/docs-html/rtl/system/ioresult.html