Where to assign TApplication properties?

I am trying to assign Application.OnError and Application.OnHashChange properties but I do not understand where to access Application.

I have multiple forms that I jump between by hash links, so there is no main TWebForm that is always loaded.

Is it possible to assign these properties my .dpr file?

Is this the correct way of doing it?

MyUnit.pas

unit MyUnit;

uses
  [...],
  Vcl.Forms,
  WEBLib.Forms;

interface

type
  TApplicationHelper = class
    procedure HandleHashChange(Sender: TObject; AHash: string; var Handled: boolean);
    procedure HandleError(Sender: TObject; AError: TApplicationError; var Handled: boolean);
  end;

procedure AssignHelper;

implementation

procedure TApplicationHandlerHelper.HandleHashChange(Sender: TObject; AHash: string; var Handled: boolean);
begin
  // do stuff
end;

procedure TApplicationHandlerHelper.HandleError(Sender: TObject; AError: TApplicationError; var Handled: boolean);
begin
   // do stuff
end;

procedure AssignHelper;
var
  h: TApplicationHelper;
  app: TApplication;
begin
  h := TApplicationHelper.Create;
  app := Application;
  app.OnHashChange := h.HandleHashChange;
  app.OnError := h.HandleError;
end;

end.

Projekt.dpr

program ProjBoknWeb;

{$R *.dres}


uses
  Vcl.Forms,
  WEBLib.Forms,
  FormLogin in 'Forms\FormLogin.pas';

{$R *.res}


begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.AutoFormRoute := True;

  MyUnit.AssignHelper;

  if not Application.Route then
    Application.CreateForm(TFormLogin, fLogin);

  Application.Run;

end.

Tried to minify the example as much as I could.

Application is a global object defined in WEBLib.Forms

You can assign it from another unit when you include WEBLib.Forms with for example:

Application.OnError := MyErrorHandler;

MyErrorHandler - Can this be a procedure in a unit not part of a class as long as the parameters are correct?

Given this example:

unit AUnit;

interface

uses
  WEBLib.Forms,


procedure Test;
procedure MyErrorHandler(Sender: TObject; AHash: string; var Handled: boolean);

implementation

procedure Test;
begin
  Application.OnHashChange := MyErrorHandler;
end;

procedure MyErrorHandler(Sender: TObject; AHash: string; var Handled: boolean);
begin
  // do stuff
end;

end.

RAD Studio says E2009 incompatiable types: 'method and regular procedure' at line LL (L:RR)

Compiling gives [Error] AUnit.pas(RR): procedural type modifier "of Object" mismatch

I am sorry if this the solution is obvious to an experienced Delphi user, I don't understand hand to do it any other way than the previous example.

The OnError event is declared as:

TApplicationErrorEvent = procedure(Sender: TObject; AError: TApplicationError; var Handled: boolean) of object;

so, it needs to be an object method.

1 Like

Very much an I-dont-know-enough-Delphi-error. :slight_smile:

Here is how to do it:

unit Navigation;

interface

uses
  WEBLib.Forms;

type
  TNavigationManager = class
    procedure HandleHashChange(Sender: TObject; AHash: string; var Handled: boolean);
  end;

var
  NavigationManager: TNavigationManager;

implementation

{ TNavigationManager }

procedure TNavigationManager.HandleHashChange(Sender: TObject; AHash: string; var Handled: boolean);
begin
  // Handle hash change here
end;

initialization

NavigationManager := TNavigationManager.Create;

end.

First create a unit with a class for handling hash changes.
The key part here was creating initializating the variable in the initialization block.

With this, the variable is created just by including the unit file in the uses block (which I did not know). Now one can simply assign Application.OnHashChange := NavigationManager.HandleHashChange in the dpr file (or anywhere with the right uses).

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.