Forum > General

Assigning methods to events in runtime

(1/2) > >>

bdexterholland:
Hi there!

I'm facing a problem that 'til now I can't find a good anwser for me.

Trying to assign a method to a TBitBtn.OnClick makes fpc raise this error:

--- Quote ---unt_dialogo.pp(204,25) Error: Incompatible types: got "<procedure variable type of procedure(const TObject) of object;Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"
--- End quote ---

What am I doing wrong?

Thanks for the help...

my code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit unt_dialogo; {$mode objfpc}{$H+} interface type  TTipoDialogo = (dtInformacao, dtAviso, dtErro, dtPergunta);  TRetornoDialogo = (rdOK, rdSim, rdNao, rdCancelar); function ShowDialog(const AMessage: string;  const ATipoDialog: TTipoDialogo = dtInformacao;  const Login: string = ''): TRetornoDialogo; implementation   uses  Forms, Graphics, Classes, Controls, Buttons, StdCtrls, ExtCtrls, SysUtils; const  _PN_TITULO: integer = $00DF9254;  _PN_CORPO: integer = $00F9F9F9;  _PN_RODAPE: integer = $00EFEFEF;  _LB_TITULO: integer = $00FFFFFF; type   { TDialogForm }   TDialogForm = class(TForm)  private    _Type: TTipoDialogo;    _Message: string;    _User: string;    _System: string;    _CodigoErro: string;     _iconeDialogo: TImage;    _headerPanel: TPanel;    _bodyPanel: TPanel;    _footerPanel: TPanel;    _titleLabel: TLabel;    _messageLabel: TLabel;    _okButton: TBitBtn;    _cancelButton: TBitBtn;    _yesButton: TBitBtn;    _noButton: TBitBtn;     procedure setYesNoButtons;    procedure setOkButton;    procedure setOkCancelButtons;     procedure setDTInformacao;    procedure setDTAViso;    procedure setDTErro;    procedure setDTPergunta;     procedure PYesButton(const Sender: TObject);    procedure PNoButton(const Sender: TObject);    procedure PCancelButton(const Sender: TObject);    procedure POkButton(const Sender: TObject);   public    constructor Create(const ASistema: string = 'NAO INFORMADO');    function ShowModal(const Atipo: TTipoDialogo; const AMensagem: string; const AUsuario: string = 'NÃO INFORMADO'): TModalResult;                procedure InicializarSubobjetos;  end; { TDialogForm }var  DialogForm: TDialogForm; function ShowDialog(const AMessage: string; const ATipoDialog: TTipoDialogo;  const Login: string): TRetornoDialogo;begin        if not(Assigned(DialogForm)) then    begin            DialogForm := TDialogForm.CreateNew(Nil);      DialogForm.InicializarSubobjetos;    end;   case DialogForm.ShowModal(ATipoDialog, AMessage, Login) of    mrYes: Result := rdSim;    mrNo: Result := rdNao;    mrCancel: Result := rdCancelar;    else      Result := rdOK;  end;end; procedure TDialogForm.setYesNoButtons;begin  _yesButton.Visible := True;  _noButton.Visible := True;  _cancelButton.Visible := False;  _okButton.Visible := False;end; procedure TDialogForm.setOkButton;begin  _yesButton.Visible := False;  _noButton.Visible := False;  _cancelButton.Visible := False;  _okButton.Visible := True;end; procedure TDialogForm.setOkCancelButtons;begin  _yesButton.Visible := False;  _noButton.Visible := False;  _cancelButton.Visible := True;  _okButton.Visible := True;end; constructor TDialogForm.Create(const ASistema: string);begin  Self._System := ASistema;end; function TDialogForm.ShowModal(const Atipo: TTipoDialogo; const AMensagem: string;  const AUsuario: string): TModalResult;begin  case Atipo of    dtInformacao: Self.setDTInformacao;    dtAviso: Self.setDTAViso;    dtErro: Self.setDTErro;    dtPergunta: Self.setDTPergunta;  end;   Self._Message := AMensagem;  Self._messageLabel.Caption := AMensagem;  Self._User := AUsuario;   Result := inherited ShowModal;   Self._Message := '';  Self._User := ''; end; procedure TDialogForm.InicializarSubobjetos;begin  {$REGION 'Formulário'}  Self.Width := 450;  Self.Height := 300;  Self.Position := poScreenCenter;  Self.BorderIcons := [];  Self.BorderStyle := bsSingle;  Self.Font.Size := 12;{$ENDREGION} {$REGION 'Paineis'}  _headerPanel := TPanel.Create(Self);        _headerPanel.Parent := Self;  _headerPanel.Align := alTop;  _headerPanel.Color := _PN_TITULO;  _headerPanel.BevelOuter := bvNone;  _headerPanel.BorderWidth := 10;  _headerPanel.Height := 50;   _bodyPanel := TPanel.Create(Self);  _bodyPanel.Parent := Self;  _bodyPanel.Align := alClient;  _bodyPanel.Color := _PN_CORPO;  _bodyPanel.BevelOuter := bvNone;  _bodyPanel.BorderWidth := 10;   _footerPanel := TPanel.Create(Self);  _footerPanel.Parent := Self;  _footerPanel.Align := alBottom;  _footerPanel.Color := _PN_RODAPE;  _footerPanel.BevelOuter := bvNone;  _footerPanel.BorderWidth := 10;  _footerPanel.Height := 55;{$ENDREGION} {$REGION 'Labels'}  _titleLabel := TLabel.Create(_headerPanel);  _titleLabel.Parent := _headerPanel;  _titleLabel.Font.Size := 16;  _titleLabel.Font.Color := clWhite;  _titleLabel.Align := alClient;  _titleLabel.Layout := tlCenter;  _titleLabel.Caption := 'Título da janela';  _titleLabel.BorderSpacing.Left := 10;   _messageLabel := TLabel.Create(_bodyPanel);  _messageLabel.Parent := _bodyPanel;  _messageLabel.Font.Color := clWindowText;  _messageLabel.Align := alLeft;  _messageLabel.Layout := tlTop;  _messageLabel.Caption := 'Texto da mensagem aparecerá aqui...';{$ENDREGION} {$REGION 'Botões'}  {TODO: Carregar ícones dos botões }  _yesButton := TBitBtn.Create(Self);        _yesButton.Parent := _footerPanel;  _yesButton.Align := alRight;  _yesButton.Caption := 'Sim';  _yesButton.Width := 100;  _yesButton.OnClick := @PYesButton;  _yesButton.BorderSpacing.Left := 10;   _noButton := TBitBtn.Create(Self);  _noButton.Parent := _footerPanel;  _noButton.Align := alRight;  _noButton.Caption := 'Não';  _noButton.Width := 100;  _noButton.OnClick := @PNoButton;  _noButton.BorderSpacing.Left := 10;   _okButton := TBitBtn.Create(Self);  _okButton.Parent := _footerPanel;  _okButton.Align := alRight;  _okButton.Caption := 'OK';  _okButton.Width := 100;  _okButton.OnClick := @POkButton;  _okButton.BorderSpacing.Left := 10;   _cancelButton := TBitBtn.Create(Self);  _cancelButton.Parent := _footerPanel;  _cancelButton.Align := alRight;  _cancelButton.Caption := 'Cancelar';  _cancelButton.Width := 100;  _cancelButton.OnClick := @PCancelButton;  _cancelButton.BorderSpacing.Left := 10;{$ENDREGION} {$REGION 'Icone da janela'}  {TODO: Adicionar ícone de acordo com o tipo de diálogo}  _iconeDialogo := TImage.Create(_headerPanel);  _iconeDialogo.Parent := _headerPanel;  _iconeDialogo.Align := alLeft;  _iconeDialogo.Width := 32;{$ENDREGION}end; procedure TDialogForm.setDTInformacao;begin  Self._titleLabel.Caption := 'Informação';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTAViso;begin  Self._titleLabel.Caption := 'Aviso';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTErro;begin  Self._titleLabel.Caption := 'Erro';  Self.Caption := Self._titleLabel.Caption;  Self.setOkButton;end; procedure TDialogForm.setDTPergunta;begin  Self._titleLabel.Caption := 'Pergunta';  Self.Caption := Self._titleLabel.Caption;  Self.setYesNoButtons;end; procedure TDialogForm.PYesButton(const Sender: TObject);begin  Self.ModalResult := mrYes;end; procedure TDialogForm.PNoButton(const Sender: TObject);begin  Self.ModalResult := mrNo;end; procedure TDialogForm.PCancelButton(const Sender: TObject);begin  Self.ModalResult := mrCancel;end; procedure TDialogForm.POkButton(const Sender: TObject);begin  Self.ModalResult := mrOk;end; finalization  DialogForm.Free; end.

Thaddy:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function ShowDialog(const AMessage: string;  const ATipoDialog: TTipoDialogo = dtInformacao;  const Login: string = ''): TRetornoDialogo of object;
Object methods have a hidden self parameter.

In this particular case I would write a type helper.

Blaazen:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---     procedure PYesButton(const Sender: TObject);    procedure PNoButton(const Sender: TObject);    procedure PCancelButton(const Sender: TObject);    procedure POkButton(const Sender: TObject);AFAIK const should not be there.

Thaddy:
That's safe enough. It is of object that really matters. Never mind I was wrong.

bdexterholland:

--- Quote from: Thaddy on January 04, 2018, 10:15:30 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function ShowDialog(const AMessage: string;  const ATipoDialog: TTipoDialogo = dtInformacao;  const Login: string = ''): TRetornoDialogo of object;
Object methods have a hidden self parameter.

In this particular case I would write a type helper.

--- End quote ---

thanks for trying but the error is not in that header. Is on line 204, and it repeats on lines 212, 220 and 228.

Navigation

[0] Message Index

[#] Next page

Go to full version