For our Guess application to function we need to add some logic. First let’s change our page a little bit. Modify the IWML:
The ServerEvents ensures that our previously created GuessButton click will remain connected. If we did not include this section the event would remain as a method, but would not be connected as an event.
ServerEvents: Clicked ]
From here on, we assume that you are fluent in Delphi and will no longer screenshot or detail every step of creating events or other routine Delphi tasks.
We will add very basic logic, and in the next sections we will pretty up our application with JQueryUI, Bootstrap, Templates, and finally show how to run our application inside a WordPress site.
Unit1.pas
Modify Unit1.pas in the following ways:
type TUnit1 = class(TIW17Page) GuessEdit: T17Edit; CountText: T17Text; MsgText: T17Text; GuessButton: T17Button; procedure GuessButtonClick(aSender: T17Renderer); private _GuessCount: Integer; _MagicNo: Integer; public end;
PageCreate Event
Select the Page (TUnit1:Unit1) in the design tree, and via object inspector create an OnCreate event and give it these contents:
procedure TUnit1.IW17PageCreate(Sender: TObject); begin _GuessCount := 1; _MagicNo := Random(100) + 1; end;
Button OnClicked Event
Now modify the GuessButton.OnClicked event we created previously:
procedure TUnit1.GuessButtonClicked(aSender: T17Renderer);
var
xGuess: Integer;
begin
MsgText.Text := '';
xGuess := StrToIntDef(Trim(GuessEdit.Text), MaxInt);
GuessEdit.Text := '';
if xGuess = MaxInt then begin
MsgText.Text := GuessEdit.Text + ' is not a valid number.'
end else if (xGuess < 1) or (xGuess > 100) then begin
MsgText.Text := 'Only numbers between 1 and 100 are valid.';
end else if xGuess = _MagicNo then begin
MsgText.Text := 'Fantastic! You guessed it in ' + _GuessCount.ToString + ' guesses.';
end else begin
if xGuess < _MagicNo then begin
MsgText.Text := xGuess.ToString + ' is too low.';
end else begin
MsgText.Text := xGuess.ToString + ' is too high.';
end;
Inc(_GuessCount);
end;
CountText.Text := 'Guess #' + _GuessCount.ToString;
end;
You can now run and use the application: