Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing event procedures runtime?

Status
Not open for further replies.

wizzor

Programmer
Aug 29, 2006
23
FI
The subject covers it,
I want to make button1 change the OnClick Event of Button2 wile running the program.

The actual problem is a little more complex, but I'll explain the rest when I can get the basics straight.
 
just define the new onclick event as the original one.

suppose the original event is Button1Click, make a second one NewButton1Click

then you can do this at runtime:

Button1.OnClick:=NewButton1Click;

Cheers,
Daddy;

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
What if I wanted to completely rewrite it?
Also, I tried it like this

procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage('Message');
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Close;
end;



procedure TForm1.BitBtn1Click(Sender: TObject);
begin
BitBtn2.OnClick := button1.click;
end;

end.

Recieved:
[Error] Unit1.pas(48): Incompatible types: 'Parameter lists differ'
No go, I suppose I did something wrong..
 
your code is wrong,

it must be

BitBtn2.OnClick := button1click; (ditch the '.')


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
OK, that helped a bit,
What if i created a button runtime, and then set a procedure to some of its events, how would I accomplish that?
 
You can do the assignment whosrdaddy suggested with any procedure as long as that procedure has a parameter list that the event is expecting. For example, an OnClick event expects a single Sender parameter of type TObject. So providing the declaration of SomeOtherProcedure is:
Code:
procedure SomeOtherProcedure(Sender: TObject);
You can make this assignment:
Code:
Button1.OnClick := SomeOtherProcedure;
and when Button1 is clicked it will execute the definition of SomeOtherProcedure. Therefore, you can assign any procedure you like to an event if the parameter list matches. Also, you could have one validation procedure assigned to all buttons of a form if you wanted too.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top