Recent

Author Topic: Change Counter from another procedure [SOLVED]  (Read 547 times)

AlphaInc.

  • Jr. Member
  • **
  • Posts: 93
Change Counter from another procedure [SOLVED]
« on: September 22, 2020, 08:44:20 pm »
Hello Everybody,

I have procedure which gets started with every start of my program that sets an initial Caption
for my TButton and a counter for said TButton to switch between different Captions. This is my Code
so mabye you get a better understanding fro what I mean.

TButton:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. begin
  3.   ShellExecute(0,nil, PChar('cmd'),PChar('/c cd Binaries && start version.bat'),nil,1);
  4.   memo1.lines.add(version[counter].msg);
  5.  
  6.   inc(counter);
  7.   if counter = 2 then counter := 0;
  8.  
  9.   Button5.Caption := version[counter].cap;
  10. end;


procedure:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.versionCheck();
  2. var
  3.   MyFile: Text;
  4.   Line1: String;
  5. begin
  6.   AssignFile(MyFile, 'config.txt');
  7.   Reset(MyFile);
  8.   ReadLn(MyFile, Line1);
  9.   CloseFile(MyFile);
  10.   if Line1 = 'version   = remastered' then
  11.     Button5.Caption := 'helloworld'
  12.   else
  13.     Button5.Caption := 'byeworld'
  14. end;

How do i set the counter of my TButton in the procedure ?
Greetings
« Last Edit: April 13, 2021, 11:19:15 am by AlphaInc. »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Change Counter from another procedure
« Reply #1 on: September 22, 2020, 09:17:43 pm »
Code snippets are always hard to interpret correctly.
You don't show where counter is declared, or the contents of your version array, or show what your batch file does to the progam's data...
The usual way to initialise variables is in the main form's OnCreate handler, whether counter is initialised directly with a constant such as zero, or whether you initialise it by reading a value from a file or database.
BTW your file handling will fall over if there is an error - always possible with files - and may make your program crash.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Change Counter from another procedure
« Reply #2 on: September 22, 2020, 09:46:49 pm »
If I understood correctly, this should be what OP wants:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.   private
  20.     FCurrent: Integer;
  21.     function VersionName(Value: Integer): string;
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. const
  28.   Filename = 'version.txt';
  29.   MaxVersion = 3;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. var
  39.   aFile:  Text;
  40.   aValue: Integer;
  41. begin
  42.  
  43.   // Load version information
  44.   FCurrent := 0;
  45.   AssignFile(aFile, Filename);
  46.   try
  47.     Reset(aFile);
  48.     ReadLn(aFile, aValue);
  49.     CloseFile(aFile);
  50.     FCurrent := aValue;
  51.   except
  52.     ShowMessage('Unable to load version info.');
  53.   end;
  54.  
  55.   // Display the version
  56.   Label1.Caption := 'You''re currently using ' + VersionName(FCurrent) + '.';
  57.  
  58.   // Set button caption
  59.   Button1.Caption := 'Click me to change to' + LineEnding +
  60.     VersionName(FCurrent+1) + LineEnding +
  61.     'on next program start.';
  62.  
  63. end;
  64.  
  65. procedure TForm1.Button1Click(Sender: TObject);
  66. var
  67.   aFile:  Text;
  68.   aValue: Integer;
  69. begin
  70.  
  71.   aValue := FCurrent + 1;
  72.   if aValue > MaxVersion then aValue := 0;
  73.  
  74.   AssignFile(aFile, Filename);
  75.   try
  76.     Rewrite(aFile);
  77.     Write(aFile, aValue);
  78.     CloseFile(aFile);
  79.   except
  80.     ShowMessage('File updating failed.');
  81.   end;
  82.  
  83. end;
  84.  
  85. function TForm1.VersionName(Value: Integer): string;
  86. begin
  87.   case Value of
  88.     0: Result := 'Prelease';
  89.     1: Result := 'Stable';
  90.     2: Result := 'Unstable';
  91.     else
  92.       Result := 'Unofficial';
  93.   end;
  94.  
  95.   // Remove this line below if you don't want to cycle back to the beginning
  96.   if Value > MaxVersion then Result := 'Prelease';
  97.  
  98.   Result := Result + ' version';
  99. end;
  100.  
  101. end.

Tested on Linux GTK2 only, but it should work on Windows too.

 

TinyPortal © 2005-2018