Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Enlazando un evento OnClick a mi componente (https://www.clubdelphi.com/foros/showthread.php?t=89036)

gustavosv 15-09-2015 21:07:56

Enlazando un evento OnClick a mi componente
 
Hola, tengo la siguiente definición de una clase que implementa un 'componente' y no sé que debo hacer para enlazar el OnClick del botón:

Código Delphi [-]
  TPruebaOnClic = class(TControl)
  private
    FRectang: TRectangle;
    FBoton: TButton;
    FOnClic: TNotifyEvent;
  public
    constructor Create(AOwner: TFmxObject);

    property OnClicBoton: TNotifyEvent read FOnClic write FOnClic;
  end;

constructor TPruebaOnClic.Create(AOwner: TFmxObject);
begin
  FRectang := TRectangle.Create(AOwner);
  with FRectang do
  begin
    Parent := AOwner;
    Align := TAlignLayout.Client;
    Opacity := 0.8;
  end;

  FBoton := TButton.Create(AOwner);
  with FBoton do
  begin
    Parent := FRectang;
    Align := TAlignLayout.Center;
    OnClick := FOnClic;
    Visible := True;
  end;
end;

Ahora desde la aplicación que usa el 'componente' ...

Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
begin
  Boton := TPruebaOnClic.Create(Panel1);
  Boton.OnClicBoton := OnClicOpc1;
end;

procedure TForm1.OnClicOpc1(Sender: TObject);
begin
  showmessage('estamos en grabar trabajo');
end;

Al ejecutarla, se muestra el rectángulo con el botón, pero no se ejecuta el evento al hacer clic sobre el botón.

No se hace de esa manera ...?

Gracias.

GustavoSV

AgustinOrtu 15-09-2015 23:25:51

TControl no publica el evento OnClick?

No tengo el Delphi a mano para verificarlo, en ese caso no hace falta que agregues el evento, con sólo publicar la propiedad OnClick ya este

Si en TControl no tiene el evento te conviene heredar de otro control que lo tenga

gustavosv 16-09-2015 01:39:12

Hola Agustín, gracias por tu respuesta, tienes razón, pero para obtenerlo necesitaría publicar todo el botón, y lo que quiero es solo dejar disponible el evento OnClick del botón.

AgustinOrtu 16-09-2015 01:48:11

No entendi eso de publicar todo el boton

Revisa este codigo:


Código Delphi [-]
unit Unit2;

interface

uses
  Vcl.Controls;

type
  TNothing = class(TControl)
  public
    property OnClick;
  end;

implementation

end.

Código Delphi [-]
procedure TForm1.FormCreate(Sender: TObject);
begin
  Nothing := TNothing.Create(Self);
  Nothing.Parent := Self;
  Nothing.Align := alClient;
  Nothing.OnClick := NothingClick;
end;

procedure TForm1.NothingClick(Sender: TObject);
begin
  ShowMessage('click on nothing');
end;

AgustinOrtu 16-09-2015 01:51:21

Releyendo tu ejemplo, lo que tenes que hacer es proveer una propiedad que mapee al OnClick del boton:


Código Delphi [-]
property OnClick: TNotifyEvent read GetOnClick write SetOnClick;

function GetOnClick: TNotifyEvent;
begin
  Result := FBoton.OnClick;
end;

procedure SetOnClick(const Value: TNotifyEvent);
begin
  FBoton.OnClick := Value
end;

gustavosv 16-09-2015 01:53:48

me refiero a publicar solo el evento OnClick del botón:
Código Delphi [-]
TNothing = class(TControl)
private
  FPanel: TPanel;
  FBoton: TButton;
public   
  property OnClick; ??? 
end;

gustavosv 16-09-2015 01:57:46

jajaja ... nos cruzamos por 1 minuto los post ...

Gracias Agustín, ahora si ya funcionó como es.

Saludos,

GustavoSV


La franja horaria es GMT +2. Ahora son las 21:43:20.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi