[Pas2js] how to access variables between various procedures

Michael Van Canneyt michael at freepascal.org
Tue Sep 15 20:28:10 CEST 2020



On Tue, 15 Sep 2020, Mgr. Janusz Chmiel wrote:

> I have helped myself by using consolebrowser unit and writeln command I have
> set MaxConsoleLines to 1 line. And in this case, I can have what I have
> wanted. If I answer good answer, app write The number of good answers. Sure,
> it look not very kind for sighted professionals, but I Am very glad, that it
> is safely possible to combine console output with HTMLwidgets unit output
> without crashes. Really. The units of Pas2js are full of positive surprices.
> Thank all of you for yours patience with Me. FOrtunately, it is possible to
> code by using various techniques. And It is excellent.

Attached is your project as you presumably intended it.

I moved the "a" variable to a field in the Tapplication class, 
and made a procedure IncScore which is called when a correct 
answer is provided. 
It updates "a" and refreshes the score text.

For this I moved the score text label from a local variable to a
field in the Tapplication class.

Tested in the browser, it works just fine.

And I made the code a little more readable for people that must see code in
order to understand it... I am amazed you manage to program at all, my
deepest respect.

Hopefully this example helps you along.

Michael.
-------------- next part --------------
program project1;
{$mode objfpc}

uses
browserapp, JS, Classes, SysUtils, Web, webwidget, htmlwidgets;



type

  TMyApplication =class(TBrowserApplication)
    a : Integer;
    tex2: TTextWidget;
    Procedure IncScore;
    procedure doRun; override;
  end;



  { TQuestion }
  TQuestion = class
     l: TLabelWidget;
     ti:TTextInputWidget;
    Question, Answer: String;
    constructor Create( _ba: TBrowserApplication; _wp: TWebPage; _Question, _Answer: String);
    procedure tiInput( _Sender : TObject; _Event : TJSEvent);
  end;

var
  Application : TMyApplication;

{ TQuestion }

constructor TQuestion.Create( _ba: TBrowserApplication; _wp: TWebPage; _Question, _Answer: String); 
begin
  Question:= _Question;
  Answer:=_Answer;
  ti:=TTextInputWidget.Create(_ba);
  l:=TLabelwidget.Create(_ba);
  l.Text:=_Question;
  l.LabelFor:= ti;
  l .Parent:= _wp;
  ti.Parent:= _wp;
  //ti.OnInput := @tiInput;
  ti.OnChange:= @tiInput;
  l .Refresh;
  ti.Refresh;
end;


procedure TQuestion.tiInput( _Sender: TObject; _Event: TJSEvent);

begin
  if ti.value = Answer then
    Application.IncScore;
end;

 Procedure TMyApplication.IncScore;

begin
  a:=a+1;
  tex2.Text:='You have answered'+IntToStr(a) +'Good answer/s from 36 possible answers';
end;

procedure TMyApplication.doRun;

var
  wp: TWebPage;
  tex: TTextWidget;

begin
  wp:=TWebPage.Create(Self);
  wp.Parent:=TViewPort.Create(Nil);
  tex:=TTextWidget.Create(Self);
  tex.Parent:=wp;
  tex.Text:='Doplňte druhého do známé dvojice. Jakmile doplníte správné jméno, aplikace Vás pochválí. V opačném případě aplikace nevypíše nic.';
  tex.Refresh;
  tex2:=TTextWidget.Create(Self);
  tex2.Parent:=wp;
  tex2.Text:='You have answered'+IntToStr(a) +'Good answer/s from 36 possible answers';
  tex2.Refresh;

 TQuestion.Create(Self, wp,'Hamlet a', 'Ofélie');
 TQuestion.Create(Self, wp,'Břetislav a', 'Jitka');
 TQuestion.Create( Self, wp,'Radůz a','Mahulena');
 TQuestion.Create( Self, wp,'Laurin a','Klement');
 TQuestion.Create( Self, wp,'Kain a','Ábel');
 TQuestion.Create( Self, wp,'Romeo a','Julie');
 TQuestion.Create( Self, wp,'Othello a','Desdemona');
 TQuestion.Create( Self, wp,'Přemysl a','Libuše');
 TQuestion.Create( Self, wp,'Kleopatra a','Antonius');
 TQuestion.Create( Self, wp,'Faust a','Markéta');
 TQuestion.Create( Self, wp,'Tristan a','Izolda');
 TQuestion.Create( Self, wp,'Adam a','Eva');
 TQuestion.Create( Self, wp,'Cyrano a','Roxana');
 TQuestion.Create( Self, wp,'David a','Goliáš');
 TQuestion.Create( Self, wp,'Oldřich a','Božena');
 TQuestion.Create( Self, wp,'Ruslan a','Ludmila');
 TQuestion.Create( Self, wp,'Kolben a','Daněk');
 TQuestion.Create( Self, wp,'Čert a','Káča');
 TQuestion.Create( Self, wp,'Romulus a','Remus');
 TQuestion.Create( Self, wp,'Hanzelka a','Zikmund');
 TQuestion.Create( Self, wp,'Suchý a','Šlitr');
 TQuestion.Create( Self, wp,'Šimek a','Grossman');
 TQuestion.Create( Self, wp,'Laurel a','Hardy');
 TQuestion.Create( Self, wp,'Voskovec a','Werich');
 TQuestion.Create( Self, wp,'Křemílek a','Vochomůrka');
 TQuestion.Create( Self, wp,'Maková panenka a','motýl Emanuel');
 TQuestion.Create( Self, wp,'Bob a','Bobek');
 TQuestion.Create( Self, wp,'Bolek a','Lolek');
 TQuestion.Create( Self, wp,'Čuk a','Gek');
 TQuestion.Create( Self, wp,'Jeníček a','Mařenka');
 TQuestion.Create( Self, wp,'Spejbl a','Hurvínek');
 TQuestion.Create( Self, wp,'Sultán a','Tyrl');
 TQuestion.Create( Self, wp,'Orfeus a','Eurydika');
 TQuestion.Create( Self, wp,'Ctirad a','Šárka');
 TQuestion.Create( Self, wp,'Samson a','Dalila');
 TQuestion.Create( Self, wp,'Cyril a','Metoděj');
  wp.Refresh;
  terminate;
end;

  
begin
  Application:=TMyApplication.Create(nil);
  Application.Initialize;
  Application.Run;
end.



More information about the Pas2js mailing list