» »

pascal copy event

pascal copy event

BRBR ::

procedure TForm1.MenuItem30Click(Sender: TObject);
var
begin
  Button1.OnClick:= self.OnClick;    
end;    



Zgleda dobro in compiler nič ne jamra, dela pa ne. Kako pripisat izvajanje kode enega elementa enemu drugemu elementu v run time ?

Jure14 ::

Torej ti v runtimu (dinamično) kreiraš gumb, ki bi mu rad določil OnClick dogodek?

Button1.OnClick := @proceduraKiSeIzvedeObKliku; 


procedure TForm1.proceduraKiSeIzvedeObKliku(Sender: TObject);
begin
...
end;

BRBR ::

Da malo popravim opis. Recimo da imam 30 gumbov + enega.
Na tem zadnjem recimo piše: 'repeat last action'
Torej, vsak klik na kateregakoli od onih prvih 30 mora zadnjemu (31), runtime pripisat izvedbo lastne procedure.

procedure TForm1.MenuItem30Click(Sender: TObject);
var
begin
  //ne dela ker je self pač form1
  Button31.OnClick:= self.OnClick;
  //dela, samo je fixno, človek pa bi rabil nekaj univerzalnega (za vseh 30 gumbov), predvidevam da preko sender ....
  //sender nima onClick ... 
  Button31.OnClick:= MenuItem30.OnClick;    
end;

Gagatronix ::

Tole mi prej deluje kot Delphi, ki je, tehnicno gledano, Pascal ki se pretvarja da zna OOP. :))

kuall ::


The OnClick method that you have written provides all the information that you
need via the Sender parameter. This is the object which is calling the method
i.e. it is the actual menu item that was clicked. If you cast the Sender
parameter as a TMenuItem you can access all of it's properties e.g.

procedure TForm1.MyOnClick( Sender : TObject );
begin
 // Check that the Sender is of the required type.
  if Sender is TMenuItem then begin
    TMenuItem( Sender ).Caption := 'You just clicked me!';
   // Or if you prefer.
    with TMenuItem( Sender ) do begin
      Enabled := False;
    end;
   // Or even..
    with Sender as TMenuItem do begin
      Checked := True;
    end;
  end;
end; 

http://www.delphigroups.info/2/4f/36076...

BRBR ::

kuall je izjavil:



The OnClick method that you have written provides all the information that you
need via the Sender parameter. This is the object which is calling the method
i.e. it is the actual menu item that was clicked. If you cast the Sender
parameter as a TMenuItem you can access all of it's properties e.g.

procedure TForm1.MyOnClick( Sender : TObject );
begin
// Check that the Sender is of the required type.
if Sender is TMenuItem then begin
TMenuItem( Sender ).Caption := 'You just clicked me!';
// Or if you prefer.
with TMenuItem( Sender ) do begin
Enabled := False;
end;
// Or even..
with Sender as TMenuItem do begin
Checked := True;
end;
end;
end;

http://www.delphigroups.info/2/4f/36076...


To bo to. Hvala.

BRBR ::

BTW

  if Sender is TMenuItem then
  begin
     Button31.OnClick:= TMenuItem(Sender).OnClick;
  end;   


Tale TMenuItem bi bil lahko nekaj višjega po hirearhiji ? Kaj ?
Tako bi if lahko odpadel ?

Zgodovina sprememb…

  • spremenil: BRBR ()

link_up ::

delphii...best memories ever. :)
In and Out

grimth ::

Spodnja koda dela univerzalno za vse komponente ki imajo OnClick event handle. Torej TButton, TMenuItem etc... . Ko klikneš gumb bRepeat bo ponovil event komponente ki je bila nazadnje pritisnjena. Ampak seveda mora imeti OnXClick event nameščen.


uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, System.Rtti,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    bRepeat: TButton;
    MainMenu1: TMainMenu;
    est1: TMenuItem;
    Button1: TButton;
    Button2: TButton;
    procedure bRepeatClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FContext: TRttiContext;
    FLastActive: TObject;
    procedure RepeatLast;
  published
    procedure onXClick(Sender : TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.bRepeatClick(Sender: TObject);
begin
  RepeatLast;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FContext := TRttiContext.Create;
  FLastActive := nil;
  est1.OnClick := onXClick;
  Button1.OnClick := onXClick;
  Button2.OnClick := onXClick;
end;

procedure TForm1.onXClick(Sender: TObject);
begin
  ShowMessage((Sender as TComponent).Name);
  if FLastActive <> Sender then
    FLastActive := Sender;
end;

procedure TForm1.RepeatLast;
var
  LProp: TRttiInstanceProperty;
  V: TValue;
  E: TNotifyEvent;
begin
   if FLastActive <> nil then
  begin
    LProp := FContext.GetType(FLastActive.ClassType).GetProperty('OnClick');
    if LProp <> nil then
    begin
      V := (LProp as TRttiInstanceProperty).GetValue(FLastActive);
      if not V.IsEmpty and V.TryAsType<TNotifyEvent>(E) then
        E(FLastActive);
    end;
  end;
end;




Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Delphi - izdelava igre

Oddelek: Programiranje
81165 (938) Nextor
»

[Delphi] Simpl vprašanja ko pasulj :)

Oddelek: Programiranje
91494 (1343) oldguy
»

Delphi - kako prepreči prečiti več zaporednih klikov gumba

Oddelek: Programiranje
51134 (1057) LoneWolf
»

Delphi problem- MessageDlg

Oddelek: Programiranje
6924 (854) c3R4R
»

Delphi problem- zapiranje programa

Oddelek: Programiranje
71159 (1105) c3R4R

Več podobnih tem