Application Code

For our Guess application to function we need to add some logic. First let’s change our page a little bit. Modify the IWML:

ACORN 1.0 IWML 1.0
TUnit1[]:Unit1
  SimpleStack[]
    Edit:GuessEdit;
    Text:MsgText;
    Text:CountText;
    Gap;
    Button:GuessButton
      ServerEvents:
        Click
      ]
      Text: Guess
    ]
  ]
]

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.

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.

Modify Unit1.pas in the following ways:

type
  TIndex = class(TIW17Page)
    GuessEdit: T17Edit;
    CountText: T17Text;
    MsgText: T17Text;
    GuessButton: T17Button;
    procedure GuessButtonClick(aSender: T17Renderer);
    procedure IW17PageCreate(Sender: TObject);
  private
    _GuessCount: Integer;
    _MagicNo: Integer;
  public
  end;

Now add a Page OnCreate event. Select the Page 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;

Now modify our existing GuessButton OnClick event:

procedure TUnit1.GuessButtonClick(aSender: T17Renderer);
var
  xGuess: Integer;
begin
  MsgText.Text := '';
  xGuess := StrToIntDef(Trim(GuessEdit.Text), MaxInt);
  GuessEdit.Text := '';

  if xGuess = MaxInt then begin
    //WebApplication.ShowMessage(GuessEdit.Text + ' is not a valid number.');
    MsgText.Text := GuessEdit.Text + ' is not a valid number.'
  end else if (xGuess < 1) or (xGuess > 100) then begin
    //WebApplication.ShowMessage('Only numbers between 1 and 100 are valid.');
    MsgText.Text := 'Only numbers between 1 and 100 are valid.';
  end else if xGuess = _MagicNo then begin
    //WebApplication.Terminate('Fantastic! You guessed it in ' + _GuessCount + ' guesses.');
    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:

The application is now functional, although still not very pretty. Let’s add a Bootstrap control to our application.