Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#4

AW: Event innerhalb einer Klasse umleiten

  Alt 24. Mär 2013, 22:16
aufs mindeste beschränkt....

Delphi-Quellcode:
unit lal;

interface

uses
  Windows, SysUtils, Variants, Classes, Graphics, Controls, ExtCtrls, StdCtrls;

type

  TMyPanel = class(TPanel)
  private
    FmyEdit: Tedit;
    function GetEvent: TKeyPressEvent;
    procedure SetEvent(const Value: TKeyPressEvent);
  public
    constructor Create(AOwner: TComponent); override;

  published
    property OnMyKeyPress: TKeyPressEvent read GetEvent Write SetEvent;
  end;

implementation

{ TMyPanel }

constructor TMyPanel.Create(AOwner: TComponent);
begin
  inherited;
  Width := 100;
  Height := 50;
  FmyEdit := Tedit.Create(self);
  FmyEdit.Parent := self;
  FmyEdit.Width := 50;
end;

function TMyPanel.GetEvent: TKeyPressEvent;
begin
  Result := FmyEdit.OnKeyPress;
end;

procedure TMyPanel.SetEvent(const Value: TKeyPressEvent);
begin
  FmyEdit.OnKeyPress := Value;
end;

end.
Anwendungsbeispiel

Delphi-Quellcode:
implementation
uses lal;
{$R *.dfm}
procedure TForm4.AKeyPressEvent(Sender: TObject; var Key: Char);
begin
   Caption := Caption + Key;
end;

procedure TForm4.Button1Click(Sender: TObject);
begin
  With TMyPanel.Create(self) do
    begin
      Parent := self;
      OnMyKeyPress := AKeyPressEvent;
    end;
end;
Da ich nicht sicher bin was genau Du suchst hier eine Version mit einer zusätzlichen internen Behandlung ...

Delphi-Quellcode:
unit lal;

interface

uses
  Windows, SysUtils, Variants, Classes, Graphics, Controls, ExtCtrls, StdCtrls;

type

  TMyPanel = class(TPanel)
  private
    FmyEdit: Tedit;
    FKeyPressEvent: TKeyPressEvent;
    procedure AInternalKeyPressEvent(Sender: TObject; var Key: Char);
  public
    constructor Create(AOwner: TComponent); override;

  published

    property OnMyKeyPress: TKeyPressEvent read FKeyPressEvent Write FKeyPressEvent;
  end;

implementation

{ TMyPanel }

procedure TMyPanel.AInternalKeyPressEvent(Sender: TObject; var Key: Char);
begin
  if Key='Athen Key := 'B';

  if assigned(FKeyPressEvent) then FKeyPressEvent(Self,Key);

end;

constructor TMyPanel.Create(AOwner: TComponent);
begin
  inherited;
  Width := 100;
  Height := 50;
  FmyEdit := Tedit.Create(self);
  FmyEdit.Parent := self;
  FmyEdit.Width := 50;
  FmyEdit.OnKeyPress := AInternalKeyPressEvent;
end;



end.
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)

Geändert von Bummi (24. Mär 2013 um 22:51 Uhr) Grund: Erweiterung, wg. möglicherweise missverstandener Anforderung
  Mit Zitat antworten Zitat